0

I have a data set as follows:

Age       d18O
  0     -35.19
 50     -35.19
 50     -34.83
100     -34.83
100     -35.05
150     -35.05
150     -35.45
200     -35.45
200     -35.42
250     -35.42
250     -35.63

I would like to get the data in the following format

Age    d18O1  d18O2
0      -35.19   NA
50     -35.19   -34.83
100    -34.83   -35.05

and so on

I tried to put it in data frame and unstack it but it didn't give me the data frame I want

any suggestions ??

Thank you

I'm not trying to transpose the data, I want to separate the duplicated values in Age into 2 separate columns

mms
  • 365
  • 1
  • 3
  • 12
  • @thelatemail I'm not trying to transpose the data ! – mms Feb 09 '16 at 04:02
  • Why does the code from the duplicate give **exactly** the output you request then - `dat$uniqid <- with(dat,ave(as.character(Age),Age,FUN=seq_along)); reshape(dat,idvar="Age",timevar="uniqid",direction="wide")` ? – thelatemail Feb 09 '16 at 04:04
  • I think this is a situation where a new question is fine - it can be harder for beginners to understand what their exact question is and the answer above can be quite intimidating – jalapic Feb 09 '16 at 04:05
  • @jalapic - I still think this is a transposition problem. The answers aren't going to get much simpler than a `dcast` or a `reshape` as per the duplicate. I'm happy to be proven wrong however. – thelatemail Feb 09 '16 at 04:10
  • @thelatemail Apologies maybe I didn't understand the code you have mentioned, but it is the same answer , I tried the code and it worked – mms Feb 09 '16 at 15:51

1 Answers1

1

you can use tidyr and dplyr

library(tidyr)
library(dplyr)

df %>% 
  group_by(Age) %>% 
  mutate(id=row_number()) %>%
  spread(id,d18O)

   Age      1      2
  (int)  (dbl)  (dbl)
1     0 -35.19     NA
2    50 -35.19 -34.83
3   100 -34.83 -35.05
4   150 -35.05 -35.45
5   200 -35.45 -35.42
6   250 -35.42 -35.63
jalapic
  • 13,792
  • 8
  • 57
  • 87