2

I have a list of data.frames. I was wondering how I could delete data.frames in this list whose names are any of the following: c("out", "Name").

I tried r[names(r) != c("out", "Name")] without success.

r <- list(
     data.frame(Name = rep("Jacob", 6), 
               X = c(2,2,1,1,NA, NA), 
               Y = c(1,1,1,2,1,NA), 
               Z = rep(3, 6), 
             out = rep(1, 6)), 

 data.frame(Name = rep("Jon", 6), 
               X = c(1,NA,3,1,NA,NA), 
               Y = c(1,1,1,2,NA,NA), 
               Z = rep(2, 6), 
             out = rep(1, 6)), 

  data.frame(Name = rep("Jon", 6), 
                X = c(1,NA,3,1,NA,NA), 
                Y = c(1,1,1,2,2,NA), 
                Z = rep(2, 6), 
              out = rep(2, 6)), 

  data.frame(Name = rep("Jim", 6), 
                X = c(1,NA,3,1,NA,NA), 
                Y = c(1,1,1,2,2,NA), 
                Z = rep(2, 6), 
              out = rep(1, 6)))
rnorouzian
  • 7,397
  • 5
  • 27
  • 72
  • Not sure what the canonical duplicate is, but something like https://stackoverflow.com/questions/25647470/filter-multiple-values-on-a-string-column-in-dplyr/25647535 looks similar or https://stackoverflow.com/questions/9665984/how-to-delete-multiple-values-from-a-vector – thelatemail Oct 06 '19 at 22:35
  • 1
    Not a dupe. Also completely changed the data. This is not about a vector at all. – rnorouzian Oct 06 '19 at 22:47
  • Since the question has been changed substantially you might want to edit the title and intro part - sounds like you're not dropping a data.frame from a list, you're dropping columns from data.frames in a list. – thelatemail Oct 06 '19 at 23:27

2 Answers2

3

We can use %in%

r[!names(r) %in% c("out", "Name")]

With the updated data

lapply(r, function(x) x[setdiff(names(x), c("out", "Name"))])
akrun
  • 874,273
  • 37
  • 540
  • 662
  • @rnorouzian Because the `list` is not named – akrun Oct 06 '19 at 22:42
  • @rnorouzian In the updated `r`, all the `list` elements are `data.frame` and have columns 'out' or 'Name'. I guess you want to subset the columns or remove the `liist` element itself if there is a match. In that case, there won't be any – akrun Oct 06 '19 at 22:56
  • @rnorouzian Okay, if you can update your post with the expected output, it would be great – akrun Oct 06 '19 at 22:58
  • @rnorouzian Going by your description `delete data.frames in this list whose names are any of the following`, all the data.frame have both names, so delete all of them? – akrun Oct 06 '19 at 22:58
  • @rnorouzian You can check `i1 <- !sapply(r, function(x) any(c("out", "Name") %in% names(x))); r[i1]` – akrun Oct 06 '19 at 23:00
  • @rnorouzian If it is based on the output of `split`, `lst1[!names(lst1) %in% c("out", "Name")]` – akrun Oct 06 '19 at 23:05
  • Arun here is my [question](https://stackoverflow.com/questions/58262597/removing-columns-based-on-a-preset-vector-of-names-in-r)? – rnorouzian Oct 07 '19 at 01:51
  • @rnorouzian I posted a solution there – akrun Oct 07 '19 at 16:09
1

Try this:

r[names(r)!='out'][names(r[names(r)!='out'])!='Name']
M--
  • 25,431
  • 8
  • 61
  • 93
anne
  • 127
  • 2
  • 4