在dplyr的mutate中是否可以使用文本模式匹配和数字替换?
这是一个玩具数据集:
library(dplyr)
df <- data.frame(Group = c("Group1", "Group1", "Group1", "Group1", "Group2", "Group2"), Activity = c("1. First really long response", "2. Second really long response", "1. First really long response", "2. Second really long response", "1. First really long response", "2. Second really long response"))
我创建了一个新的数据框,其中包含一些选择&#34; 1的受访者。首先是非常长的回应。&#34;
test <- df %>%
group_by(Group) %>% summarise(
`Participants` = length(Activity[Activity == "1. First really long response"])
)
有效。
让我们说我想避免使用对变量Activity的整个响应。 (答案真的很长。)是否有可能做以下事情(显然不起作用)?
test <- df %>%
group_by(Group) %>% summarise(
`Participants` = ifelse(grepl("1.", Activity), length(Activity)))
类似的东西:
答案 0 :(得分:4)
在R中,你经常通过求和逻辑向量来计算事物。你可以做到
df %>%
group_by(Group) %>%
summarise(Participants = sum(grepl("1.", Activity)))