在 R 中分解多个 google 表单条目
Breaking up multiple google form entries in R
我有一个 csv 文件,其中包含在线 Google 表单的结果,我正在使用该表单在 R 中创建图表。表单上有一个问题允许多个响应,人们可以在回复。 Google Forms 创建 CSV 的方式是将所有答案放在一行中。这是几行的示例:
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
我想创建一个条形图来总结对问题的所有单独回答,但为了做到这一点,我需要将每行中的每个回答分开(每个回答用逗号分隔)。有没有办法在 R 或 Python 中对此进行编码?还是我应该处理 Google 表单本身?
这是 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
从这里开始,您可以使用 ggplot
:
轻松制作条形图
ggplot(data = separated.data, aes(x = Change.in.treatment)) +
geom_bar(stat = "count")
示例数据:
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
))
我有一个 csv 文件,其中包含在线 Google 表单的结果,我正在使用该表单在 R 中创建图表。表单上有一个问题允许多个响应,人们可以在回复。 Google Forms 创建 CSV 的方式是将所有答案放在一行中。这是几行的示例:
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 |
我想创建一个条形图来总结对问题的所有单独回答,但为了做到这一点,我需要将每行中的每个回答分开(每个回答用逗号分隔)。有没有办法在 R 或 Python 中对此进行编码?还是我应该处理 Google 表单本身?
这是 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
从这里开始,您可以使用 ggplot
:
ggplot(data = separated.data, aes(x = Change.in.treatment)) +
geom_bar(stat = "count")
示例数据:
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
))