Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

Breaking up multiple google form entries in R

I have a csv file with results from an online Google Form that I am using to create graphs in R. There is one question on the form that allows for multiple responses, and people can write in a response. The way Google Forms is creating the CSV is by putting all answers into one row. Here is an example of a few rows:

Date Change in treatment
1/4 Started new medication, changed dose
1/5 Started new medication
1/6 Stopped medication, Started new medication, New diagnosis
1/7 New diagnosis

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's an approach with tidyr::separate_rows:

library(tidyverse)
separated.data <- data %>% 
  separate_rows(Change.in.treatment,sep = ", ")
separated.data
# A tibble: 7 x 2
  Date  Change.in.treatment   
  <chr> <chr>                 
1 1/4   Started new medication
2 1/4   changed dose          
3 1/5   Started new medication
4 1/6   Stopped medication    
5 1/6   Started new medication
6 1/6   New diagnosis         
7 1/7   New diagnosis  

From here, you can easily make a bar chart with ggplot:

ggplot(data = separated.data, aes(x = Change.in.treatment)) +
  geom_bar(stat = "count")

enter image description here

Sample Data:

data <- structure(list(Date = c("1/4", "1/5", "1/6", "1/7"), Change.in.treatment = c("Started new medication, changed dose", 
"Started new medication", "Stopped medication, Started new medication, New diagnosis", 
"New diagnosis")), class = "data.frame", row.names = c(NA, -4L
))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...