根据向量中的值过滤数据框行

发布于 2024-12-11 13:18:30 字数 567 浏览 0 评论 0 原文

当要删除的值存储在向量中时,从数据框中过滤行的最佳方法是什么?就我而言,我有一个包含日期的列,并且想要删除几个日期。

我知道如何使用 != 删除与 one 天相对应的行,例如:

m[m$date != "01/31/11", ]

要删除向量中指定的多个日期,我尝试过:

m[m$date != c("01/31/11", "01/30/11"), ]

但是,这会生成警告消息:

Warning message:
In `!=.default`(m$date, c("01/31/11", "01/30/11")) :
longer object length is not a multiple of shorter object length
Calls: [ ... [.data.frame -> Ops.dates -> NextMethod -> Ops.times -> NextMethod

应用基于多个值的过滤器的正确方法是什么?

What is the best way to filter rows from data frame when the values to be deleted are stored in a vector? In my case I have a column with dates and want to remove several dates.

I know how to delete rows corresponding to one day, using !=, e.g.:

m[m$date != "01/31/11", ]

To remove several dates, specified in a vector, I tried:

m[m$date != c("01/31/11", "01/30/11"), ]

However, this generates a warning message:

Warning message:
In `!=.default`(m$date, c("01/31/11", "01/30/11")) :
longer object length is not a multiple of shorter object length
Calls: [ ... [.data.frame -> Ops.dates -> NextMethod -> Ops.times -> NextMethod

What is the correct way to apply a filter based on multiple values?

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

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

发布评论

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

评论(4

扭转时空 2024-12-18 13:18:30

nzcoops 的建议很正确。我不久前在 R Chat 中提出了这个问题,Paul Teetor 建议定义一个新函数:

`%notin%` <- function(x,y) !(x %in% y) 

然后可以按如下方式使用它:

foo <- letters[1:6]

> foo[foo %notin% c("a", "c", "e")]
[1] "b" "d" "f"

不用说,这个小宝石现在在我的 R 个人资料并且经常使用。

nzcoops is spot on with his suggestion. I posed this question in the R Chat a while back and Paul Teetor suggested defining a new function:

`%notin%` <- function(x,y) !(x %in% y) 

Which can then be used as follows:

foo <- letters[1:6]

> foo[foo %notin% c("a", "c", "e")]
[1] "b" "d" "f"

Needless to say, this little gem is now in my R profile and gets used quite often.

当爱已成负担 2024-12-18 13:18:30

我想为此你想要:

m[!m$date %in% c("01/31/11","01/30/11"),]

I think for that you want:

m[!m$date %in% c("01/31/11","01/30/11"),]
桃扇骨 2024-12-18 13:18:30

很酷的方法是使用 Negate 函数来创建新的:

`%ni%` <- Negate(`%in%`) 

然后您可以使用它来查找不相交的元素

cool way is to use Negate function to create new one:

`%ni%` <- Negate(`%in%`) 

than you can use it to find not intersected elements

北音执念 2024-12-18 13:18:30

针对上面的一些问题,这里有一个符合 tidyverse 的解决方案。我使用 dplyr 中的 anti_join 来达到相同的效果:

library(tidyverse)

numbers <- tibble(numbers = c(1:10))
numbers_to_remove <- tibble(number = c(3, 4, 5))

numbers %>%
  anti_join(numbers_to_remove)

In regards to some of the questions above, here is a tidyverse compliant solution. I used anti_join from dplyr to achieve the same effect:

library(tidyverse)

numbers <- tibble(numbers = c(1:10))
numbers_to_remove <- tibble(number = c(3, 4, 5))

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