R函数在同一列中搜索和计算多个条件?

标签 r filter dplyr pipe multiple-conditions

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

例如,我想计算出每个人的特定值组合(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/

相关文章:

使用来自不同数据的 auto.arima 的参数对数据运行 ARIMA 模型

r - 错误 : ModuleNotFoundError: No module named 'keras'

R如何计算两行之间的 'Percentage of Change'?

r - 使用聚合的 na.omit 和 na.pass 的混合?

java - 按主键/外键搜索时启用 Hibernate 过滤器

javascript - 如何使用过滤器删除数组中的对象? AngularJS

reactjs - 不带下拉菜单的 Ant 表自定义过滤器复选框

r - dplyr 覆盖组中除第一次出现以外的所有值

r - 根据逗号分割数据框列

r - 如何添加到 group_by_at dplyr 函数中的分组