I want to go from long to wide format. My data kind of looks like this:
day=c(1,2,3,4,5,6,1,2,3,4,5,6)
site=c('a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'b')
value.1=c(1,2,5,7,5,3,9,4,2,8,1,8)
value.2=c(5,4,7,6,2,4,6,9,4,2,5,6)
data=data.frame(day,site,value.1,value.2)
> data
day site value.1 value.2
1 1 a 1 5
2 2 a 2 4
3 3 a 5 7
4 4 a 7 6
5 5 a 5 2
6 6 a 3 4
7 1 b 9 6
8 2 b 4 9
9 3 b 2 4
10 4 b 8 2
11 5 b 1 5
12 6 b 8 6
I want to switch it into wide format based on site. So it looks like this
> data
day a.value.1 a.value.2 b.value.1 b.value.2
1 1 1 5 9 6
2 2 2 4 4 9
3 3 5 7 2 4
4 4 7 6 8 2
5 5 5 2 1 5
6 6 3 4 8 6
I feel like I should be able to do this with the reshape
package, but I cant figure it out
I would love some help with this. Thank you
You could do this in base R
reshape(data, idvar='day', timevar='site',direction='wide')
# day value.1.a value.2.a value.1.b value.2.b
#1 1 1 5 9 6
#2 2 2 4 4 9
#3 3 5 7 2 4
#4 4 7 6 8 2
#5 5 5 2 1 5
#6 6 3 4 8 6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With