根据R中的名称从data.frame列表中删除data.frame

时间:2019-10-06 22:06:33

标签: r list function loops dataframe

我有一个data.frames列表。我想知道如何删除该列表中名称为以下任意一项的data.frame:c("out", "Name")

我尝试r[names(r) != c("out", "Name")]失败了。

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

2 个答案:

答案 0 :(得分:2)

我们可以使用%in%

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

使用更新的数据

lapply(r, function(x) x[setdiff(names(x), c("out", "Name"))])

答案 1 :(得分:0)

尝试一下:

  

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