0

I have the following data.

ID      Date        Day             Sedentary   Light
4701    04/02/2013  Monday          511.5        133.7
4701    08/02/2013  Friday          370.2        113.0
4702    04/02/2013  Monday          405.5        119.2
4702    05/02/2013  Tuesday         601.8        162.5
4702    06/02/2013  Wednesday       463.0        121.7
4702    07/02/2013  Thursday        472.3        149.2
4702    08/02/2013  Friday          383.2        106.5
4704    06/02/2013  Wednesday       594.7        153.8
4704    07/02/2013  Thursday        616.0        115.0

I would like to use the ID value in column 1 to transpose the data so that the data for each unique ID number is transposed from rows into columns i.e.

ID      D1date      D1day   D1Sedentary D1Light D2date      D2day   D2Sedentary D2Light
4701    04/02/2013  Monday  511.5       133.7   08/02/2013  Friday  511.5       133.7

The number of rows that require transposing for each ID number range from 1-7.

Many thanks for any help from a novice R user.

Ash
  • 237
  • 2
  • 6
  • 16
  • In the future, please supply your data using `dput(data)`, where `data` is your dataframe. This makes it much easier for other users to quickly help you. – Thomas Feb 28 '14 at 15:07
  • Thank you Thomas and apologies I'd never heard of 'dput'. – Ash Feb 28 '14 at 15:15
  • You can read more about how to use it on Stack Overflow here: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Thomas Feb 28 '14 at 15:18

1 Answers1

0
df$Date <- ave(as.numeric(df$Date), df$ID, FUN=seq_along)
reshape(df, timevar=names(df)[2], idvar=names(df)[1], direction="wide")
lukeA
  • 53,097
  • 5
  • 97
  • 100