ibm
ibm

Reputation: 864

Using str_count for multiple strings for "check all that apply" in survey data

Qualtrics' check all that applies produces per respondent a comma-separated cell with all the options each person clicked on. If I make a count table I get something like this, but with 10 unique choices:

Combinations Count
for news 32
for news, talk to friends 7
for news, find information 14
for news, talk to friends, find information 5
talk to friends 55

I want to count each option so that for news = 32+7+14+5, talk to friends = 7+5+55 etc. I can do df$Var %>% str_count("for news") %>% table() but this would have to be done 10 times. I am getting an error trying to mutate ("Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "factor")

df$Var%>%mutate(news = str_count("for news"),
friends = str_count("friends"),
info = str_count("information"))

Do I have to str_extract using the comma, creating individual columns, and then pivot_long or is there a way to make different str_counts in one go?

Upvotes: 1

Views: 294

Answers (3)

ibm
ibm

Reputation: 864

df %>%
  separate_rows(Var, sep = ',\\s*') %>%
  group_by(Var) %>% tally()

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388907

Split the comma separated strings in new rows using separate_rows and for each Combinations , sum it's count.

library(dplyr)
library(tidyr)

df %>%
  separate_rows(Combinations, sep = ',\\s*') %>%
  group_by(Combinations) %>%
  summarise(Count = sum(Count))

#  Combinations     Count
#  <chr>            <int>
#1 find information    19
#2 for news            58
#3 talk to friends     67

data

It is easier to help if you provide data in a reproducible format -

df <- structure(list(Combinations = c("for news", "for news, talk to friends", 
"for news, find information", "for news, talk to friends,find information", 
"talk to friends"), Count = c(32L, 7L, 14L, 5L, 55L)), row.names = c(NA, 
-5L), class = "data.frame")

Upvotes: 1

Kra.P
Kra.P

Reputation: 15123

if space between each respondent, (for example, not for news, talk to friends,find information, like for news, talk to friends, find information) and let your data as df,

x <- rep(df$Combinations, df$Count)
x %>% str_plit(., ", ") %>% unlist %>% table

find information         for news  talk to friends 
              19               58               67  

or by using str_count,

y <- c("for news", "talk to friends", "find information")
sapply(y, function(X) str_count(x, X)) %>% colSums()
str_count(x, "for news")

    for news  talk to friends find information 
          58               67               19 

Upvotes: 1

Related Questions