问题描述
我有3个data.frame
的列表:
my_list <- list(a = data.frame(value = c(1:5), class = c(letters[1:3],"a", "b")), b = data.frame (value = c(6:1),class=c(letters[1:4],"a", "b")),c=data.frame(value = c(1:7),class = c(letters[5:1],"a", "b")))
my_list
$a
value class
1 1 a
2 2 b
3 3 c
4 4 a
5 5 b
$b
value class
1 6 a
2 5 b
3 4 c
4 3 d
5 2 a
6 1 b
$c
value class
1 1 e
2 2 d
3 3 c
4 4 b
5 5 a
6 6 a
7 7 b
我想进入每个列表,并按class
列中的字母a
和b
对其进行子集设置:
I want to go in to each list and subset them by letters a
and b
from the class
column:
wanted_sub_class <- c("a", "b")
,然后将结果放入每个class
的my_list
列表中.
and then put the results in a list of my_list
per class
.
编辑-预期输出:
$a class a
value class
1 a
4 a
$a class b
value class
2 b
5 b
$b class a
value class
4 a
2 a
$b class b
value class
5 b
1 b
$c class a
value class
5 a
6 b
$c class b
value class
4 b
7 b
我试图用双循环来做到这一点:
I've tried to do it with a double loop:
result <- list()
for (i in 1:length(my_list)) {
for (j in wanted_sub_class {
result [[i]] <- subset(my_list[[i]], my_list[[i]]$class == j)
}
}
这应该给我6个列表元素(根据预期的输出),但是它只给出3个元素,并且仅给出元素b
.
This should give me 6 list elements (as per expected output) but it only gives 3 and only of element b
.
但是理想情况下,如果实际可行,我希望将结果放入每个class
的my_list
列表中.因此,我想在列表中保留这3个data.frames的结构,然后在其中包含一个数据类别为a
和b
的列表-否则,六个列表将起作用
Ideally, however, if it's actually possible, I want to put the results in a list of my_list
per class
. So I want to keep the structure of the 3 data.frames in the list and then have a list with in that with the data of class a
and b
- Otherwise, a list of six will work
我知道循环并不是理想的方法,但是我无法真正实现环绕声(例如使用lapply).对于循环(如果可能)和向量化的答案,我将不胜感激.
I understand loops aren't ideal but I can't really get my head around vecortisation (e.g. using lapply). I would appreciate an answer for both loop (if it's possible) and vectorization.
推荐答案
如果我们使用的是Hadleyverse系列软件包中的purrr
If we are using purrr
from the Hadleyverse family of packages
library(purrr)
my_list %>%
map(~ .[.$class %in% wanted_sub_class,])
#$a
# value class
#1 1 a
#2 2 b
#$b
# value class
#1 4 a
#2 3 b
#$c
# value class
#4 4 b
#5 5 a
或者如果输出只需要包含'a'和'b'list
元素
library(dplyr)
my_list %>%
bind_rows %>%
filter(class %in% wanted_sub_class) %>%
split(., .$class)
#$a
# value class
#1 1 a
#3 4 a
#6 5 a
#$b
# value class
#2 2 b
#4 3 b
#5 4 b
更新
基于OP的更新
Update
Based on the OP's update
my_list %>%
map(~ .[.$class %in% wanted_sub_class,]) %>%
map(~split(.x, seq_len(nrow(.x)))) %>%
do.call("c", .)
#$a.1
# value class
#1 1 a
#$a.2
# value class
#2 2 b
#$b.1
# value class
#1 4 a
#$b.2
# value class
#2 3 b
#$c.1
# value class
#4 4 b
#$c.2
# value class
#5 5 a
或使用bind_rows
方法
my_list %>%
bind_rows %>%
filter(class %in% wanted_sub_class) %>%
split(., seq_len(nrow(.)))
Update2
如果需要for
循环
result <- setNames(vector('list', length(my_list)), names(my_list))
for(i in seq_along(my_list)){
result[[i]] <- subset(my_list[[i]], class %in% wanted_sub_class)
result[[i]] <- split(result[[i]], 1:nrow(result[[i]]))
}
Update3
对于新的输出格式
Update3
For the new output format
my_list %>%
bind_rows(.id = "id") %>%
filter(class %in% wanted_sub_class) %>%
split(., list(.$id, .$class))
或使用for
循环
result <- setNames(vector('list', length(my_list)), names(my_list))
for(i in seq_along(my_list)){
result[[i]] <- subset(my_list[[i]], class %in% wanted_sub_class)
result[[i]] <- split(result[[i]], result[[i]]$class, drop = TRUE)
}
这篇关于使用循环(或向量化)通过向量中的多个元素对列表进行子集化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!