如何在单行数据集上使用 spread 和 group_by

How do I use spread and group_by on a single row dataset

我有一个表单数据框,其中有多个相同 IDsdates 的条目。 我需要将此数据集分组为一行,但我在使用 gather、spread 和 group 时遇到了一些问题。

# surveys dataset
user_id <- c(100, 100, 100, 200, 200, 200)
int_id <- c(1000, 1000, 1000, 2000, 2000, 2000)
fech <- c('01/01/2019', '01/01/2019','01/01/2019','02/01/2019','02/01/2019','02/01/2019')
order <- c(1,2,3,1,2,3)
questions <- c('question1','question2','question3','question1','question2','question3')
answers <- c('answ1','answ2','answ3','answ1','answ2','answ3')

survey.data <- data.frame(user_id, int_id, fech, order, questions,answers)

> survey.data
  user_id int_id       fech order questions answers
1     100   1000 01/01/2019     1 question1   answ1
2     100   1000 01/01/2019     2 question2   answ2
3     100   1000 01/01/2019     3 question3   answ3
4     200   2000 02/01/2019     1 question1   answ1
5     200   2000 02/01/2019     2 question2   answ2
6     200   2000 02/01/2019     3 question3   answ3

我使用 spread 将一些列变成行:

survey.data %>% 
  spread(key= questions, value=answers) %>%
  group_by(user_id,int_id, fech) %>% 
  select(-order) 

并得到以下内容:

# A tibble: 6 x 6
  user_id int_id       fech question1 question2 question3
*   <dbl>  <dbl>     <fctr>    <fctr>    <fctr>    <fctr>
1     100   1000 01/01/2019     answ1        NA        NA
2     100   1000 01/01/2019        NA     answ2        NA
3     100   1000 01/01/2019        NA        NA     answ3
4     200   2000 02/01/2019     answ1        NA        NA
5     200   2000 02/01/2019        NA     answ2        NA
6     200   2000 02/01/2019        NA        NA     answ3

我尝试对生成的数据集进行分组,但总是得到 6 行而不是 2 行。

我预期如下:

user_id int_id  fech        question1 question2 question3
 100   1000     01/01/2019     answ1     answ2     answ3
 200   2000     02/01/2019     answ1     answ2     answ3

我的问题与非常相似!

但我不知道如何使用它。

我认为您需要在分组前删除 order:

survey.data %>% 
  select(-order) %>% 
  # group_by(user_id, int_id, fech) %>% # as pointed out, unnecessary
  spread(questions, answers)

结果:

# A tibble: 2 x 6
# Groups:   user_id, int_id, fech [2]
  user_id int_id fech       question1 question2 question3
    <int>  <int> <chr>      <chr>     <chr>     <chr>    
1     100   1000 01/01/2019 answ1     answ2     answ3    
2     200   2000 02/01/2019 answ1     answ2     answ3 

我发现(我认为)另一种可能的解决方案:

survey.data %>% 
  select(-order) %>% 
  dcast(... ~ questions)
´´´