Daniel
Daniel

Reputation: 435

dplyr: Conditional mutate using text (grep?)

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:

  1. Look in the Activity variable for something that starts with "1."
  2. If you find it, mark as TRUE.
  3. Count how many total TRUE's there are in the new Participants variable.

Upvotes: 0

Views: 1180

Answers (1)

MrFlick
MrFlick

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

Related Questions