r - 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"
解决方案
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
推荐阅读
- wordpress - 在 Wordpress 上创建一个新的下载页面
- powershell - PowerShell 提示 - 分别显示用户和系统路径
- python - 如何计算python函数的时间复杂度(渐近符号)?
- flask - Flask 中的 Celery 任务未完成
- slack - Zapier Slack 私人频道:仅当用户在频道中时才发送消息
- quickfix - Quickfixn c# MarketDataSnapshotFullRefresh 但无法获取市场数据
- reactjs - React Axios - 延迟 GET 方法
- javascript - 为什么 Axios Delete 给我一个 404 错误?
- react-native-android - 无法构造转换器:DuplicateError:重复的文件或模拟。请检查控制台以获取更多信息
- mongodb - 将 MongoDB 集群迁移到另一个帐户