I need help transforming a long df to a wide one.
I think this is a basic questions, but I'm stucked.
After reading about ´reshape2´, I've used this:
dcast(a, day + sessions ~ hour)
And all possible variations.
But didn't get the expected result.
This is my starting data frame "a":
head(a)
day hour sessions
1 Sun 0 785
2 Sun 1 354
3 Sun 2 190
4 Sun 3 121
5 Sun 4 105
6 Sun 5 110
Expected result: I need to have this format:
day 0 1 2 3 ... 23
1 Sun 123 454 23 43 ... 56
2 Mon 56 354 778 76 ....89
3 Thur 75 190 653 533 ....87
4 Wen 3 121 45 54 ....77
5 Tue 4 105 21 44 ....52
6 Fri 5 110 12 21 ....51
str of df "a":
'data.frame': 168 obs. of 3 variables:
$ day : Ord.factor w/ 7 levels "Sun"<"Mon"<"Tues"<..: 1 1 1 1 1 1 1 1 1 1 ...
$ hour : Factor w/ 24 levels " 0"," 1"," 2",..: 1 2 3 4 5 6 7 8 9 10 ...
$ sessions: num 785 354 190 121 105 110 185 258 252 416 ...
dcast(a, day ~ hour)
– ialm