-2

I wanted to transform repeated measure values into new columns in R. For example, for df =

ID       time    
0001     11.3
0001     14.7
0008     14.3
0008     8.2
0013     11.3
0013     12.9
0013     5.1

Desired output would be to collapse ID (individuals) and transform available repeated measures into additional columns.

ID    time_1  time_2  time_3    
0001  11.3    14.7    NA     
0008  14.3    8.2     NA
0013  11.3    12.9    5.1

I edited my initial question. I was not able to find the correct way to do it as stated above.

sleepyjoe
  • 243
  • 1
  • 2
  • 10
  • 2
    "This is probably difficult" and your lack of code make it seem like you haven't attempted to solve this yourself. This isn't a code writing service. – nrussell Feb 02 '16 at 00:22
  • 1
    It's bordering on the impossible, oh wait, no it isn't - `reshape(dat, idvar="ID", direction="wide", timevar="visit", sep="_")` – thelatemail Feb 02 '16 at 00:36
  • Thank you thelatemail. But I still could not get to the right df using reshape. – sleepyjoe Feb 02 '16 at 01:08

1 Answers1

1

We can do this with dcast

library(reshape2)
dcast(df1, ID~ paste('time', visit, sep='_'), value.var='time')
#     ID time_1 time_2 time_3
#1 0001   11.3   14.7     NA
#2 0008   14.3    8.2     NA
#3 0013   11.3   12.9    5.1
akrun
  • 789,025
  • 32
  • 460
  • 575