首页 > 解决方案 > R函数在同一列中搜索和计算多个条件?

问题描述

有没有办法在同一列中搜索多个条件,然后计算出现次数?

例如,我想弄清楚每个人的特定值组合(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"

标签: rfilterdplyrpipemultiple-conditions

解决方案


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

推荐阅读