I need to turn this
id | amount | day
---------------------
A | 10 | 0
A | 54 | 8
A | 23 | 18
A | 43 | 28
A | 87 | 51
B | 34 | 0
B | 76 | 1
B | 12 | 7
into this
id | a1 | a2 | a3 | a4 | a5 | d1 | d2 | d3 | d4 | d5
--------------------------------------------------------
A | 10 | 54 | 23 | 43 | 87 | 0 | 8 | 18 | 28 | 51
B | 34 | 76 | 12 | 0 | 0 | 0 | 1 | 7 | 0 | 0
ie. transpose/cast the rows of the df to an unknown number of columns by id, putting zeros where there would be empty values because of the inequal length.
I’ve experimented with
df <- data.frame(id=c('A','A','A','A','A','B','B','B'), amount=c(10,54,23,43,87,34,76,12), day=c(0,8,18,28,51,0,1,7))
library(reshape2)
x <- dcast(df, id ~ day, mean, value = 'amount')
but it’s not quite right. How do I do it?
Use base R
reshape()after introducing a “time” variable:Optional steps:
Reorganize column order:
Replace
NAwith0: