从列表中删除数据框,如果不满足条件。

huangapple go评论58阅读模式
英文:

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 &lt;- 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) &quot;ID&quot; %in% colnames(x))


for (i in sequence(my_list)) {
  
  if (sapply(my_list, function(x) &quot;ID&quot; %in% colnames(x)) == FALSE) {
    DROP THE df2 and df3
  }
} 

答案1

得分: 3

Filter()函数从base包中提取列表中逻辑函数返回true的元素。

Filter(\(df) &quot;ID&quot; %in% colnames(df), my_list)

使用purrr的等效选项是keep/discard

purrr::keep(my_list, ~ &quot;ID&quot; %in% colnames(.x))
purrr::discard(my_list, ~ !&quot;ID&quot; %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) &quot;ID&quot; %in% colnames(df), my_list)

Equivalent options with purrr are keep/discard:

purrr::keep(my_list, ~ &quot;ID&quot; %in% colnames(.x))
purrr::discard(my_list, ~ !&quot;ID&quot; %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) &quot;ID&quot; %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(!&quot;ID&quot; %in% names(my_list[[nm]])) my_list[[nm]] &lt;- 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, ~ &quot;ID&quot; %in% colnames(.))]
$df1
  ID Name
1  1    a
2  2    b
3  3    c
4  4    d
5  5    e

huangapple
  • 本文由 发表于 2023年5月6日 15:32:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76187665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定