我有一个 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
}
} )
我得到的错误可能是在sapply
的append
部分,也可能是在lapply
的索引中。我试了一下,但仍然找不到错误(或者更好的方法)。
2条答案
按热度按时间ndasle7k1#
如果使用
sapply/lapply
,则应避免尝试更改全局变量的值。相反,应返回所需的值。例如,如果要将列表中的每个项目的ID作为列表移除,则生成一个向量然后,您可以将该列表与您的数据列表和
mapply
一起使用,以便一起迭代这两个列表kzmpq1sx2#
使用
base R