如何过滤嵌套数据
如何过滤嵌套的数据集(确保巢与某些参考向量或tibble完全相同)?
library(tidyverse)
rev_vec <- c("apple", "pear", "banana")
df <- tibble(
ID= rep(1:3, each =3),
fruits = c("apple", "pear", "banana",
"Pineapple", "Pineapple", "orange",
"lime", "pear", NA))
df_vec <- df %>%
group_by(ID) %>%
summarise(fruits = list(unique(fruits)))
## This does not work
df_vec %>%
filter(fruits == rev_vec)
## This does not work
df_vec %>%
filter(unlist(fruits) == rev_vec)
## This does not work
df_vec %>%
filter(all(unlist(fruits[[1]]) ==rev_vec))
基本上,我只需要知道哪个ID(在这种情况下1)匹配参考向量
预期结果
ID 1与Rev Vec匹配。
df_vec %>%
filter(....)
# A tibble: 1 x 2
ID fruits
<int> <list>
1 1 <chr [3]>
How can I filter a nested dataset (make sure the nest is the exact same as some reference vector or tibble)?
library(tidyverse)
rev_vec <- c("apple", "pear", "banana")
df <- tibble(
ID= rep(1:3, each =3),
fruits = c("apple", "pear", "banana",
"Pineapple", "Pineapple", "orange",
"lime", "pear", NA))
df_vec <- df %>%
group_by(ID) %>%
summarise(fruits = list(unique(fruits)))
## This does not work
df_vec %>%
filter(fruits == rev_vec)
## This does not work
df_vec %>%
filter(unlist(fruits) == rev_vec)
## This does not work
df_vec %>%
filter(all(unlist(fruits[[1]]) ==rev_vec))
Basically, I just need to know which ID (in this case 1) matches the reference vector
expected outcome
Only ID 1 matches the rev vec.
df_vec %>%
filter(....)
# A tibble: 1 x 2
ID fruits
<int> <list>
1 1 <chr [3]>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(3)
不确定您希望输出结构化,但这是
另一个想法,
Not sure how you want the output structured, but here is an idea
Another output,
也许您可以尝试使用
相同的
来查看每个iD> ID
的水果
是否与参考向量完全相同。输出
Perhaps you could try using
identical
to see if thefruits
for eachID
are exactly identical to the reference vector.Output