Search code examples
rspread

Repeated columns a into new row


I have repeated values in column a and I want these to become new row with information from column b.

I've tried the tidyr function for gather and spread

library("tidyr")
rearrangeddf<-spread(df,a,b)


#Input
a=c("A","A","A","A","A","B","B","B","B","B")
b=c(1,2,3,4,5,11,12,13214634,14,15432)
df=data.frame(a,b)

#Output
x=c("A",1,2,3,4,5)
y=c("B",11,12,13214634,14,1543)
rearrangeddf=rbind(x,y)

Error: Each row of output must be identified by a unique combination of keys. Keys are shared for 10 rows: * 1, 2, 3, 4, 5 * 6, 7, 8, 9, 10 Do you need to create unique ID with tibble::rowid_to_column()? Call rlang::last_error() to see a backtrace


Solution

  • You can use

    aggregate(b~a, df, c)
      a      b.1      b.2      b.3      b.4      b.5
    1 A        1        2        3        4        5
    2 B       11       12 13214634       14    15432
    

    Not part of answer

    Do not use c=c("A",1,2,3,4,5) because this will overwrite the c() function. See here:

    c=c("A",1,2,3,4,5)
    aggregate(b~a, df, c)
    Error in get(as.character(FUN), mode = "function", envir = envir) : 
      object 'FUN' of mode 'function' was not found