英文:
Remove data frame from list if doesn’t satisfy a condition
问题
I have a list object below. I would like to remove df2
and df3
from the list since they do not have an "ID" column. how to get around this? many thanks in advance.
my_list <- list(df1 = data.frame(ID = 1:5, Name = letters[1:5]),
df2 = matrix(c(1, 2, 3, 4), ncol = 2),
df3 = data.frame(Name = letters[6:10], Age = c(20, 25, 30, 35, 40)))
sapply(my_list, function(x) "ID" %in% colnames(x))
for (i in sequence(my_list)) {
if (sapply(my_list, function(x) "ID" %in% colnames(x)) == FALSE) {
DROP THE df2 and df3
}
}
英文:
I have a list object below. I would like to remove df2
and df3
from the list since they do not have an "ID" column. how to get around this? many thanks in advance.
my_list <- list(df1 = data.frame(ID = 1:5, Name = letters[1:5]),
df2 = matrix(c(1, 2, 3, 4), ncol = 2),
df3 = data.frame(Name = letters[6:10], Age = c(20, 25, 30, 35, 40)))
sapply(my_list, function(x) "ID" %in% colnames(x))
for (i in sequence(my_list)) {
if (sapply(my_list, function(x) "ID" %in% colnames(x)) == FALSE) {
DROP THE df2 and df3
}
}
答案1
得分: 3
Filter()
函数从base
包中提取列表中逻辑函数返回true
的元素。
Filter(\(df) "ID" %in% colnames(df), my_list)
使用purrr
的等效选项是keep/discard
:
purrr::keep(my_list, ~ "ID" %in% colnames(.x))
purrr::discard(my_list, ~ !"ID" %in% colnames(.x))
输出
$df1
ID Name
1 1 a
2 2 b
3 3 c
4 4 d
5 5 e
英文:
Filter()
from base
extracts the elements of a list for which a logical function gives true.
Filter(\(df) "ID" %in% colnames(df), my_list)
Equivalent options with purrr
are keep/discard
:
purrr::keep(my_list, ~ "ID" %in% colnames(.x))
purrr::discard(my_list, ~ !"ID" %in% colnames(.x))
Output
$df1
ID Name
1 1 a
2 2 b
3 3 c
4 4 d
5 5 e
答案2
得分: 2
你可以使用普通的逻辑子集:
my_list[sapply(my_list, function(x) "ID" %in% colnames(x))]
英文:
You can use normal logical subsetting:
my_list[sapply(my_list, function(x) "ID" %in% colnames(x))]
答案3
得分: 2
使用 for
循环
for(nm in names(my_list)) if(!"ID" %in% names(my_list[[nm]])) my_list[[nm]] <- NULL
英文:
Using a for
loop
for(nm in names(my_list)) if(!"ID" %in% names(my_list[[nm]])) my_list[[nm]] <- NULL
</details>
# 答案4
**得分**: 1
另一种选项是使用布尔向量来对列表进行子集化。这里我们使用 `map_lgl`:
```R
library(purrr)
my_list[map_lgl(my_list, ~ "ID" %in% colnames(.))]
$df1
ID Name
1 1 a
2 2 b
3 3 c
4 4 d
5 5 e
英文:
Another option is using boolean vector to subset the list. Here we use map_lgl
:
library(purrr)
my_list[map_lgl(my_list, ~ "ID" %in% colnames(.))]
$df1
ID Name
1 1 a
2 2 b
3 3 c
4 4 d
5 5 e
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论