[R] R Data Manipulation - Transposing Data by a Given Column, Like User_ID

Eik Vettorazzi E.Vettorazzi at uke.uni-hamburg.de
Thu Feb 3 21:52:44 CET 2011


Hi Mike
reshape will be your friend.

reshape(RAW,direction="wide",timevar="SITE",idvar="USER_ID")


there is also the 'reshape'-package, which can do some more
sophisticated transformations.

hth.

Am 03.02.2011 20:41, schrieb Mike Schumacher:
> Hello,
> 
> I'd like to transpose data to create an analysis-friendly dataframe.  See
> below for an example, I was unable to use t(x) and I couldn't find a
> function with options like PROC TRANSPOSE in SAS.
> 
> The ideal solution handles variable quantities of SITE - but beggars can't
> be choosers.  :-)
> 
> Thank you in advance,
> 
> Mike
> 
> ## INPUT DATA
> USER_ID<-c(1,1,1,2,2,2,3,3,4)
> SITE
> <-c("SITE1","SITE2","SITE3","SITE1","SITE2","SITE3","SITE1","SITE2","SITE3")
> COUNTS <-c(10,13,22,10,12,12,13,44,99)
> 
> RAW<-data.frame(USER_ID,SITE,COUNTS)
> RAW
> 
> #ANSWER SHOULD LOOK LIKE
> a<-c(1,2,3,4)
> b<-c(10,10,13,0)
> c<-c(13,12,44,0)
> d<-c(22,12,0,99)
> 
> RESULT<-data.frame(a,b,c,d)
> names(RESULT)<-c("USER_ID","SITE1","SITE2","SITE3")
> RESULT
> 
> 
> 
> 


-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790



More information about the R-help mailing list