使用sapply获取向量,并使用该向量通过lapply从列表中的 Dataframe 中删除行

ykejflvf  于 2023-01-15  发布在  其他
关注(0)|答案(2)|浏览(93)

我有一个 Dataframe 列表:

df1 <- data.frame(id = seq(1:10), name = LETTERS[1:10])
df2 <- data.frame(id = seq(11:20), name = LETTERS[11:20])
mylist <- list(df1, df2)

我想根据一个条件(在本例中,存储在id列中的值)从列表中的每个 Dataframe 中删除行。我创建一个空向量,在其中存储id:

ids_to_remove <- c()

然后我应用我的函数:

sapply(mylist, function(df) {
  
  rows_above_th <- df[(df$id > 8),] # select the rows from each df above a threshold
  a <- rows_above_th$id # obtain the ids of the rows above the threshold 
  ids_to_remove <- append(ids_to_remove, a) # append each id to the vector
  
},

simplify = T

)

但是,不管有没有simplify = T,它都返回一个矩阵,而我想要的输出(ids_to_remove)将是一个包含ids的向量,如下所示:

ids_to_remove <- c(9,10,9,10)

因为最后我将以这种方式在单个 Dataframe 上使用它:

for(i in 1:length(ids_to_remove)){

                  mylist[[1]] <- mylist[[1]] %>%
                    filter(!id == ids_to_remove[i])

                }

就像整个列表上的这个(这是行不通的,我不明白为什么):

i = 1
lapply(mylist, 
       function(df) {
         
                for(i in 1:length(ids_to_remove)){
                  df <- df %>%
                    filter(!id == ids_to_remove[i])
                           
                  i = i + 1
         
                }
} )

我得到的错误可能是在sapplyappend部分,也可能是在lapply的索引中。我试了一下,但仍然找不到错误(或者更好的方法)。

ndasle7k

ndasle7k1#

如果使用sapply/lapply,则应避免尝试更改全局变量的值。相反,应返回所需的值。例如,如果要将列表中的每个项目的ID作为列表移除,则生成一个向量

ids_to_remove <- lapply(mylist, function(df) {
  rows_above_th <- df[(df$id > 8),] # select the rows from each df above a threshold
  rows_above_th$id # obtain the ids of the rows above the threshold
})

然后,您可以将该列表与您的数据列表和mapply一起使用,以便一起迭代这两个列表

mapply(function(data, ids) {
  data %>% dplyr::filter(!id %in% ids)
}, mylist, ids_to_remove, SIMPLIFY=FALSE)
kzmpq1sx

kzmpq1sx2#

使用base R

Map(\(x, y) subset(x, !id %in% y), mylist, ids_to_remove)

相关问题