r - rearrange the output of the aggregate function into a new table -
r - rearrange the output of the aggregate function into a new table -
i have info set describes locations. aggregate response metric y within locations according categorical predictor, p. type of dataframe output
location<-c('site1','site1','site1','site2','site2','site3','site3','site3','site3') p<-c('a','b','c','a','b','a','b','c','d') y<-c(1,2,3,1,2,1,2,3,4) data.frame(location,p,y) what want info frame looks this
p<-c('a','b','c','d') site1<-c(1,2,3,na) site2<-c(1,2,na,na) site3<-c(1,2,3,4) data.frame(p,site1,site2,site3) is there simple way in r?
df1 <- data.frame(location,p,y) library(reshape2) dcast(df1, p ~ location, value.var = "y") ## p site1 site2 site3 ## 1 1 1 1 ## 2 b 2 2 2 ## 3 c 3 na 3 ## 4 d na na 4
r
Comments
Post a Comment