Reputation: 435
Is it possible to use text pattern matching with numerical replacement while in dplyr's mutate?
Here's a toy dataset:
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"))
I create a new dataframe with a count of respondents who selected "1. First really long response."
test <- df %>%
group_by(Group) %>% summarise(
`Participants` = length(Activity[Activity == "1. First really long response"])
)
That works.
Let's say I want to avoid using the whole response to the variable Activity. (The responses are really quite long.) Is it possible to do something like the following (which obviously doesn't work)?
test <- df %>%
group_by(Group) %>% summarise(
`Participants` = ifelse(grepl("1.", Activity), length(Activity)))
Something like:
Upvotes: 0
Views: 1180
Reputation: 206242
In R you often count things by summing a logical vector. You can just do
df %>%
group_by(Group) %>%
summarise(Participants = sum(grepl("1.", Activity)))
Upvotes: 4