1

i have a data table, which contains three variables:

  1. hours: format is int, value in the range of [0,23], and it is increased
  2. mins: format is int, value is (10,20,30,40,50,60), it is also increased
  3. x: format is int

below is a simple sample:

stocks <- data.frame(
hours = c(0,0,0,0,0,0),
mins = c(10,10,10,20,20,30),
x = c(2,4,4,5,3,4)
)

output:

based on this table, i want to add more multiple columns according to hours and mins. it looks like this as below:

    0_10 0_20 0_30
     2    5    4
     4    3    
     4        

I tried to use the dcast function, but the final table just calculate the frequency of the X :(

library(data.table)
dcast(setDT(stocks), x ~ hours+mins, value.var = c("x")) 
#Aggregate function missing, defaulting to 'length'
   x 0_10 0_20 0_30
1: 2    1    0    0
2: 3    0    1    0
3: 4    2    0    1
4: 5    0    1    0

any suggestion ?

thanks !

Frank
  • 66,179
  • 8
  • 96
  • 180
ZAWD
  • 651
  • 7
  • 31
  • Try `lst <- unstack(stocks, x~hours + mins);data.frame(lapply(lst, 'length<-', max(lengths(lst))))` – akrun Nov 03 '16 at 09:22

1 Answers1

2

We need to change the formula in dcast

library(data.table)#1.9.7+
dcast(setDT(stocks), rowid(hours, mins)~hours+mins, value.var = "x")[, hours := NULL][]
#   0_10 0_20 0_30
#1:    2    5    4
#2:    4    3   NA
#3:    4   NA   NA

With versions < 1.9.7, we create the sequence variable grouped by 'hours', 'mins', and then do the dcast

setDT(stocks)[, Seq := 1:.N, by = .(hours, mins)]
dcast(stocks, Seq~hours + mins, value.var = "x")[, Seq := NULL][]
akrun
  • 874,273
  • 37
  • 540
  • 662