删除列表中多个数据帧的特定行

发布于 2024-11-28 18:50:47 字数 400 浏览 1 评论 0原文

假设我有这样一个列表,其中包括 3 个名为 1、3 和 4 的数据框:

        1                   3           4  
1   A   c(2, 1, 3, 1, 2)    c(1, 1, 2)  c(1, 1)
2   B   c(1, 1, 1, 3, 2)    c(2, 1, 2)  c(2, 1)

数据框具有所有相同的列(A 和 B),但行数不同,如您所见。如何删除值 < 的行B 列中的 2 对于列表中的所有数据框?

我尝试 lapply 与任何:

list <- lapply(list, function(x) {x <- any(x[,c(2)] < 2);x})

Lets suppose I have such a list including 3 dataframes named 1, 3 and 4:

        1                   3           4  
1   A   c(2, 1, 3, 1, 2)    c(1, 1, 2)  c(1, 1)
2   B   c(1, 1, 1, 3, 2)    c(2, 1, 2)  c(2, 1)

The dataframes have all the same columns (A and B) but different counts of rows as you see. How do I erase the rows which have values < 2 in column B for all dataframes in the list?

I tried lapply with any:

list <- lapply(list, function(x) {x <- any(x[,c(2)] < 2);x})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过气美图社 2024-12-05 18:50:48

明智地使用 lapply() 和简单的子集设置与任何方法一样好。在 l 中使用您的数据:

l <- list("1" = data.frame(A = c(2, 1, 3, 1, 2), B = c(1, 1, 1, 3, 2)),
          "3" = data.frame(A = c(1,1,2), B = c(2,1,2)),
          "4" = data.frame(A = c(1,1), B = c(2,1)))

这可以实现您想要的功能

lapply(l, function(x) x[x$B >= 2,])

> lapply(l, function(x) x[x$B >= 2,])

明智地使用 lapply() 和简单的子集设置与任何方法一样好。在 l 中使用您的数据:

l <- list("1" = data.frame(A = c(2, 1, 3, 1, 2), B = c(1, 1, 1, 3, 2)),
          "3" = data.frame(A = c(1,1,2), B = c(2,1,2)),
          "4" = data.frame(A = c(1,1), B = c(2,1)))

这可以实现您想要的功能

lapply(l, function(x) x[x$B >= 2,])

1` A B 4 1 3 5 2 2

明智地使用 lapply() 和简单的子集设置与任何方法一样好。在 l 中使用您的数据:

l <- list("1" = data.frame(A = c(2, 1, 3, 1, 2), B = c(1, 1, 1, 3, 2)),
          "3" = data.frame(A = c(1,1,2), B = c(2,1,2)),
          "4" = data.frame(A = c(1,1), B = c(2,1)))

这可以实现您想要的功能

lapply(l, function(x) x[x$B >= 2,])

3` A B 1 1 2 3 2 2

明智地使用 lapply() 和简单的子集设置与任何方法一样好。在 l 中使用您的数据:

l <- list("1" = data.frame(A = c(2, 1, 3, 1, 2), B = c(1, 1, 1, 3, 2)),
          "3" = data.frame(A = c(1,1,2), B = c(2,1,2)),
          "4" = data.frame(A = c(1,1), B = c(2,1)))

这可以实现您想要的功能

lapply(l, function(x) x[x$B >= 2,])

4` A B 1 1 2

Judicious use of lapply() and simple subsetting is as good as any approach. Using your data in l:

l <- list("1" = data.frame(A = c(2, 1, 3, 1, 2), B = c(1, 1, 1, 3, 2)),
          "3" = data.frame(A = c(1,1,2), B = c(2,1,2)),
          "4" = data.frame(A = c(1,1), B = c(2,1)))

This does what you want

lapply(l, function(x) x[x$B >= 2,])

giving:

> lapply(l, function(x) x[x$B >= 2,])

Judicious use of lapply() and simple subsetting is as good as any approach. Using your data in l:

l <- list("1" = data.frame(A = c(2, 1, 3, 1, 2), B = c(1, 1, 1, 3, 2)),
          "3" = data.frame(A = c(1,1,2), B = c(2,1,2)),
          "4" = data.frame(A = c(1,1), B = c(2,1)))

This does what you want

lapply(l, function(x) x[x$B >= 2,])

giving:

1` A B 4 1 3 5 2 2

Judicious use of lapply() and simple subsetting is as good as any approach. Using your data in l:

l <- list("1" = data.frame(A = c(2, 1, 3, 1, 2), B = c(1, 1, 1, 3, 2)),
          "3" = data.frame(A = c(1,1,2), B = c(2,1,2)),
          "4" = data.frame(A = c(1,1), B = c(2,1)))

This does what you want

lapply(l, function(x) x[x$B >= 2,])

giving:

3` A B 1 1 2 3 2 2

Judicious use of lapply() and simple subsetting is as good as any approach. Using your data in l:

l <- list("1" = data.frame(A = c(2, 1, 3, 1, 2), B = c(1, 1, 1, 3, 2)),
          "3" = data.frame(A = c(1,1,2), B = c(2,1,2)),
          "4" = data.frame(A = c(1,1), B = c(2,1)))

This does what you want

lapply(l, function(x) x[x$B >= 2,])

giving:

4` A B 1 1 2
一江春梦 2024-12-05 18:50:48

像这样的事情怎么样:

lst <- lapply(lst, function(x) {subset(x, B >= 2)})

How about something like this:

lst <- lapply(lst, function(x) {subset(x, B >= 2)})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文