Casting with more than one R value variable

I want to switch from long to wide format. My data is as follows:

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 to a wide format based on the site. So it looks like

> 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

It seems to me that I can do this with the package reshape, but I can not understand it

I would like to help with this. Thanks you

+4
source share
4 answers

You can do it 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
+4
source

You can use packages tidyrand dplyr:

library("tidyr")
library("dplyr")
data %>% 
  gather(labs, values, value.1:value.2) %>% 
  unite(site2, site,labs, sep = ".") %>% 
  spread(site2, values) 

Although I have to admit that there are not many reasons, it reshapeworks quite well. I just like tidyru dplyr.

+4
source

dcast, , . melt , dcast :

> data.long = melt(data, id = c("day", "site"))
> head(data.long)
  day site variable value
1   1    a  value.1     1
2   2    a  value.1     2
3   3    a  value.1     5
4   4    a  value.1     7
5   5    a  value.1     5
6   6    a  value.1     3
> dcast(data.long, day ~ site + variable)
  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
+2
source

Yes, this can be done using the 'reshape' package:

library(reshape)

# Melt the data to obtain the 'value' labels
dM <- melt(data, id.vars=c('day', 'site'))

# Now concatenate the site and 'value' labels
dM$siteVal <- paste(dM$site, dM$variable, sep=".")

# Cast the data using site-value labels
dSite <- cast(dM, day ~ siteVal)
0
source

All Articles