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

Gabor Grothendieck ggrothendieck at gmail.com
Fri Feb 4 05:18:37 CET 2011


On Thu, Feb 3, 2011 at 10:05 PM, Peter Ehlers <ehlers at ucalgary.ca> wrote:
> On 2011-02-03 14:09, Dennis Murphy wrote:
>>
>> Hi:
>>
>> This also works, except that the result is of class 'table':
>>
>> xtabs(COUNTS ~ USER_ID + SITE, data = RAW)
>>        SITE
>> USER_ID SITE1 SITE2 SITE3
>>       1    10    13    22
>>       2    10    12    12
>>       3    13    44     0
>>       4     0     0    99
>>
>> If you need a data frame, then the earlier responses apply.
>
> Agreed. But just for fun, since this is R:
>
>  z <- xtabs(COUNTS ~ USER_ID + SITE, data = RAW)
>  d <- as.data.frame(unclass(z))
>
> That'll lose the USER_ID variable; if that's needed:
>
>  d <- as.data.frame(cbind(USER_ID=row.names(z), unclass(z)))
>

Try this:

cbind( dimnames( z )[ 1 ], as.data.frame( z[ ] ) )



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list