rnorouzian
rnorouzian

Reputation: 7517

Drop a data.frame from a list of data.frames based on names in R

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)))

Upvotes: 2

Views: 984

Answers (2)

anne
anne

Reputation: 127

Try this:

r[names(r)!='out'][names(r[names(r)!='out'])!='Name']

Upvotes: 1

akrun
akrun

Reputation: 886978

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"))])

Upvotes: 3

Related Questions