tidyr spread does not aggregate data

痴心易碎 提交于 2019-12-06 05:57:45

问题


I have data of the following:

    > data <- data.frame(unique=1:9, grouping=rep(c('a', 'b', 'c'), each=3), value=sample(1:30, 9))
    > data
      unique grouping value
    1      1        a    15
    2      2        a    21
    3      3        a    26
    4      4        b     8
    5      5        b     6
    6      6        b     4
    7      7        c    17
    8      8        c     1
    9      9        c     3

I would like to create a table that looks like this:

       a        b    c
1      15       8    17
2      21       6    1
3      26       6    3

I am using tidyr::spread and not getting the correct result:

> data %>% spread(grouping, value)
  unique  a  b  c
1      1 15 NA NA
2      2 21 NA NA
3      3 26 NA NA
4      4 NA  8 NA
5      5 NA  6 NA
6      6 NA  4 NA
7      7 NA NA 17
8      8 NA NA  1
9      9 NA NA  3

Or

> data %>% select(grouping, value) %>% spread(grouping, value)
Error: Duplicate identifiers for rows (1, 2, 3), (4, 5, 6), (7, 8, 9)

Is there a way to do this also when one group (c) has a different length than the others?


回答1:


We need to create a sequence column to avoid the duplicate identifiers row Error.

library(tidyr)
library(dplyr)
data %>% 
    group_by(grouping) %>% 
    mutate(id = row_number()) %>% 
    select(-unique) %>%
    spread(grouping, value) %>%
    select(-id)
#     a     b     c
#  (int) (int) (int)
#1    15     8    17
#2    21     6     1
#3    26     4     3


来源:https://stackoverflow.com/questions/35370885/tidyr-spread-does-not-aggregate-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!