有没有办法在同一列中搜索多个条件,然后统计出现的次数?
例如,我想计算出每个人的特定值组合(x 然后 y,x 然后 w,x 然后 z)相继出现了多少次。
我尝试编写 IF 语句,但被告知 dplyr 将是更好的路线。
Dataframe:
c1 c2
person1 x
person1 y
person1 a
person1 a
person2 x
person2 w
person1 x
person1 z
df %>% select(c1, c2)
%>% tally(filter(c2 == "x")
%>% lead(filter(c2=="y")))
预期结果:显示每个人出现 x 然后 y、x 然后 w、x 然后 z 的总次数的子集。
c1 xy xw xz
Person 1 1 0 1
Person 2 0 1 0
R 给出以下错误:
Error in UseMethod("filter_") :
no applicable methord for 'filter_' applied to an object of class
"logical"
最佳答案
library(dplyr)
c1 = c("person1",
"person1",
"person1",
"person1",
"person2",
"person2",
"person1",
"person1")
c2 = c("x","y","a","a","x","w","x","z")
df = as_tibble(cbind(c1,c2))
df %>%
group_by(c1) %>%
summarise(xy = sum(c2 == "x" & lead(c2, 1) == "y" ),
xw = sum(c2 == "x" & lead(c2, 1) == "w"),
xz = sum(c2 == "x" & lead(c2, 1) == "z"))
给你
# A tibble: 2 x 4
c1 xy xw xz
<chr> <int> <int> <int>
1 person1 1 0 1
2 person2 0 1 0
关于R函数在同一列中搜索和计算多个条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57575105/