Получить таблицу пропорций из данных опроса - PullRequest
0 голосов
/ 08 мая 2019

У меня есть данные опроса для 30+ вопросов по 5-балльной шкале Лайкерта (полностью согласен с категорически не согласен)

Вот некоторые примеры данных:

survey_df <- data.frame("ID" = c(1,2,3,4,5),
                        "Q1" = c("Strongly Agree", "Strongly Agree", "Agree", "Agree", "Neither"),
                        "Q2" = c("Agree", "Strongly Disagree", "Disagree", "Agree", "Neither"),
                        "Q3" = c("Neither", "Neither", "Disagree", "Agree", "Neither"))

Так что в основном я хочуперейти от этого:

ID Q1             Q2                Q3
1  Strongly Agree Agree             Neither
2  Strongly Agree Strongly Disagree Neither
3  Agree          Disagree          Disagree
4  Agree          Agree             Agree
5  Neither        Neither           Neither

К этому:

Question Strongly.Agree Agree Neither Disagree Strongly.Disagree N.Count
Q1       0.4            0.4   0.2     0.0      0.0               5
Q2       0.0            0.4   0.2     0.2      0.2               5
Q3       0.0            0.2   0.6     0.2      0.0               5

1 Ответ

2 голосов
/ 08 мая 2019

Вам нужно использовать функции gather() и spread() из библиотеки tidyr , чтобы транспонировать ваш фрейм данных, а затем использовать mutate() и mutate_at() (из dplyr *) 1008 *) для расчета N.Count и пропорции по ответу.

library(tidyverse)

survey_df %>% 
  gather(-ID, key = 'Question', value = 'Answer') %>% 
  count(Question, Answer) %>% 
  spread(key = Answer, value = n, fill = 0) %>% 
  mutate(N.Count = Agree + Disagree + Neither + `Strongly Agree` + `Strongly Disagree`) %>% 
  mutate_at(vars(-Question, -N.Count), funs(. / N.Count))

#   Question Agree Disagree Neither `Strongly Agree` `Strongly Disagree` N.Count
#   <chr>    <dbl>    <dbl>   <dbl>            <dbl>               <dbl>   <dbl>
# 1 Q1         0.4      0       0.2              0.4                 0         5
# 2 Q2         0.4      0.2     0.2              0                   0.2       5
# 3 Q3         0.2      0.2     0.6              0                   0         5
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...