[英]R: Filter a Dataframe with a list on matching values of a list-column
I'm new to R and have a Problem I just don't seem to get solved..我是 R 的新手并且有一个问题我似乎没有得到解决..
I have a table like this:我有一张这样的桌子:
issue | teams
-------------
A | team-A,team-B
B | team-C
C | team-A
D | team-A,team-B
E | team-B
The column teams is of the type "list" Now I have another "list" with teams the current user is in. For example "team-B,team-C" .列团队是“列表”类型现在我有另一个“列表”,其中包含当前用户所在的团队。例如“team-B,team-C” 。 What I want to achive is to filter the dataframe that it only shows rows where the teams the current user is in have at least on match in the teams-column.我想要实现的是过滤数据框,它只显示当前用户所在的团队至少在团队列中匹配的行。 In this examle the output should be:在这个例子中,输出应该是:
issue | teams
-------------
A | team-A,team-B
B | team-C
D | team-A,team-B
E | team-B
I tried with intersect which gives me on a length-check a boolean ("length(intersect(my_teams,teams)).= 0").我尝试使用 intersect,它给了我一个长度检查布尔值(“长度(intersect(my_teams,teams))。= 0”)。 But if I use this in a dplyr-Filter it doesn't work, I think I didn't understand the data-structures with lists, vectors.但是,如果我在 dplyr-Filter 中使用它,它就不起作用,我想我不理解带有列表、向量的数据结构。 dataframes and so on fully and their different behaviour in dataframes.数据帧等以及它们在数据帧中的不同行为。 As I said I'm just starting with R?正如我所说,我只是从 R 开始? Could someone give me a hand or hint how I can solve this filtering, I'm sure it's pretty easy, but I'm out of google-Skills ^^有人可以帮助我或提示我如何解决此过滤问题,我相信这很容易,但我没有 google-Skills ^^
Thanks in Advance!提前致谢!
If we have a list
column, we can loop over the list
with sapply
to create a logical vector
for subset
ting如果我们有一个list
列,我们可以使用sapply
list
,为subset
创建一个逻辑vector
v1 <- c("team-B", "team-C")
subset(df1, sapply(teams, \(x) any(v1 %in% x)))
-output -输出
issue teams
1 A team-A, team-B
2 B team-C
4 D team-A, team-B
5 E team-B
Or using tidyverse
或者使用tidyverse
library(dplyr)
library(purrr)
df1 %>%
filter(map_lgl(teams, ~ any(v1 %in% .x)))
issue teams
1 A team-A, team-B
2 B team-C
3 D team-A, team-B
4 E team-B
df1 <- structure(list(issue = c("A", "B", "C", "D", "E"), teams = list(
c("team-A", "team-B"), "team-C", "team-A", c("team-A", "team-B"
), "team-B")), row.names = c(NA, -5L), class = "data.frame")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.