R: spread function on data frame with duplicates

前端 未结 2 764
南旧
南旧 2021-01-20 21:59

I have a data frame that I need to pivot but the data frame has duplicate identifiers, so spread function gives an error Error: Duplicate identifiers for

2条回答
  •  粉色の甜心
    2021-01-20 22:14

    You could use dcast from the devel version of data.table ie. v1.9.5. Instructions to install are here

    library(data.table)#v1.9.5+
    dcast(setDT(df), Dimension~Date, value.var='Metric', 
                   fun.aggregate=function(x) toString(unique(x)))
    #   Dimension  Fri Mon Tue Wed
    #1:         A 7, 8  23  25    
    #2:         B        7       9
    

    Or

    library(dplyr)
    library(tidyr)
    df %>%
       group_by(Dimension, Date) %>% 
       summarise(Metric=toString(unique(Metric))) %>% 
       spread(Date, Metric, fill='')
    #   Dimension  Fri Mon Tue Wed
    #1         A 7, 8  23  25    
    #2         B        7       9
    

    Update

    Using the new dataset from `OP's post

     setDF(df2)
     df2 %>% 
         group_by(Dimension, Date) %>% 
         summarise(Metric=toString(unique(Metric))) %>%
         spread(Date, Metric, fill='') %>%
         head(2) %>%
         select(1:3)
     #    Dimension 16 analog tuner
     #1 10994030020  9             
     #2 12300245685            NTSC
    

提交回复
热议问题