Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Counting based on partial strings in R

I’m working with some survey data collected from a google form. One of the questions allowed for respondents to provide multiple answers:

Data:

id_number <- c("101", "102", "103", "104", "105", "106")

why_join_program <- c("college assistance", 
                     "college assistance, fasfa support", 
                     "fasfa support, employment support", 
                     "college assistance, fasfa support, employment support", 
                     "college assistance, employment support",
                     "fasfa support")


df <- data.frame(id_number, why_join_program)

I have two questions:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. I want to count how many responses include a single answer (i.e., how many respondents identified "college support"?

  2. Can you provide a good way to organize this into a table? group_by and summarize() creates a table that counts every combination of responses. I want to create a table broken down by response and count the number of respondents who indicated that answer. This table would count respondents multiple times if they indicated multiple answers.

Thank you.

>Solution :

Using strsplit and table.

strsplit(df$why_join_program, ', ') |> unlist() |> table()
# college assistance employment support      fasfa support 
#                  4                  3                  4 

Note: R >= 4.1 used

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading