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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to