[R] Maintaining repeated ID numbers when transposing with reshape
Adaikalavan Ramasamy
a.ramasamy at imperial.ac.uk
Tue Aug 26 00:05:54 CEST 2008
There might be a more elegant way of doing this but here is a way of
doing it without reshape().
df <- data.frame( ID=c(1,1,1,1,2,2),
TEST=c("A","A","B","C","B","B"),
RESULT=c(17,12,15,12,8,9) )
df.s <- split( df, df$ID )
out <- sapply( df.s, function(m)
tapply( m$RESULT, m$TEST, paste, collapse="," ) )
t(out)
A B C
1 "17,12" "15" "12"
2 NA "8,9" NA
Not the same output as you wanted. This makes more sense unless you have
a reason to priotize 17 instead of 12 in the first row.
Regards, Adai
jcarmichael wrote:
> I have a dataset in "long" format that looks something like this:
>
> ID TEST RESULT
> 1 A 17
> 1 A 12
> 1 B 15
> 1 C 12
> 2 B 8
> 2 B 9
>
> Now what I would like to do is transpose it like so:
>
> ID TEST A TEST B TEST C
> 1 17 15 12
> 1 12 . .
> 2 . 8 .
> 2 . 9 .
>
> When I try:
>
> reshape(mydata, v.names="result", idvar="id",timevar="test",
> direction="wide")
>
> It gives me only the first occurrence of each test for each subject. How
> can I transpose my dataset in this way without losing information about
> repeated tests?
>
> Any help or guidance would be appreciated! Thanks!
More information about the R-help
mailing list