简体   繁体   English

从 R 中的数据框列表中删除特定数据框

[英]Remove specific data frames from a list of data frames in R

I have read and used the answers to the following question ( Remove specific data frame from a list of data frames in R ), where one specific data frame was to be removed from a list.我已阅读并使用了以下问题的答案( 从 R 中的数据框列表中删除特定数据框),其中要从列表中删除一个特定数据框。 I now need to build on this but am struggling to find the right solution.我现在需要以此为基础,但正在努力寻找正确的解决方案。 I have a list of 48 data frames and would like to remove several items from the list, is there a similar code I can use?我有一个包含 48 个数据框的列表,想从列表中删除几个项目,是否有类似的代码可以使用?

Using the same example as the similar question, if to remove $d2 is my.list = my.list[-2] , how would I remove $d2, $d3 and $d6 - ideally at the same time, or one at a time?使用与类似问题相同的示例,如果删除 $d2 是my.list = my.list[-2] ,我将如何删除 $d2、$d3 和 $d6 - 理想情况下是同时删除,或者一次删除一个时间?

Writing the same code consecutively doesn't appear to work, eg.连续编写相同的代码似乎不起作用,例如。

my.list = my.list[-2]
my.list = my.list[-3] #or should this be [-2] as [3] has become [2] when i removed the original [2]
my.list = my.list[-6] #same again



my.list
$d1
  y1 y2
1  1  4
2  2  5
3  3  6

$d2
  y1 y2
1  3  6
2  2  5
3  1  4

$d3
  y1 y2
1  1  4
2  2  5
3  3  6

$d4
  y1 y2
1  3  6
2  2  5
3  1  4

$d5
  y1 y2
1  1  4
2  2  5
3  3  6

$d6
  y1 y2
1  3  6
2  2  5
3  1  4

There are a variety of valid answers in the comments using numeric indices.使用数字索引的评论中有各种有效答案。 Here's yet another:这是另一个:

my.list2 <- my.list[ !    #negation operator
                         names(my.list) %in% c('d2','d3','d6') ]  #logical index
#check
> my.list2
$d1
  y1 y2
1  1  4
2  2  5
3  3  6

$d4
  y1 y2
1  3  6
2  2  5
3  1  4

$d5
  y1 y2
1  1  4
2  2  5
3  3  6

The reason your attempt failed is that the sequence of values in my.list changed as soon as you removed the first item.您的尝试失败的原因是my.list的值序列在您删除第一项后立即更改。 Notice that I assigned to a different named item.请注意,我分配给了一个不同的命名项。 That's a much safer strategy.这是一个更安全的策略。

The reason I added a demonstration of a logical indexing scheme is its superior generality.我添加一个逻辑索引方案的演示的原因是它具有优越的通用性。 You can instead define a criterion that you might use on each item in turn using sapply .您可以改为使用sapply定义您可能在每个项目上使用的sapply The logical vector that results can be used as a selection vector.结果的逻辑向量可用作选择向量。

Example for testing:测试示例:

dput(my.list)
list(d1 = structure(list(y1 = 1:3, y2 = 4:6), class = "data.frame", row.names = c("1", 
"2", "3")), d2 = structure(list(y1 = 3:1, y2 = 6:4), class = "data.frame", row.names = c("1", 
"2", "3")), d3 = structure(list(y1 = 1:3, y2 = 4:6), class = "data.frame", row.names = c("1", 
"2", "3")), d4 = structure(list(y1 = 3:1, y2 = 6:4), class = "data.frame", row.names = c("1", 
"2", "3")), d5 = structure(list(y1 = 1:3, y2 = 4:6), class = "data.frame", row.names = c("1", 
"2", "3")), d6 = structure(list(y1 = 3:1, y2 = 6:4), class = "data.frame", row.names = c("1", 
"2", "3")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM