0

I have a dataframe like this one

familyid Year  memberid count var
1        2000  1        2     5
1        2000  1        2     6
1        2000  2        1     8
2        2000  1        1     5
2        2000  2        1     4
3        2000  1        1     5
3        2000  2        2     7
3        2000  2        2     5

where the column count indicates how many times each observation compares in the dataframe. I want to transpose the dataframe only for those comparing more than one times, in other words I want to have

familyid     Year  memberid count var_1  var_2
    1        2000  1        2     5      6
    1        2000  2        1     8      NA
    2        2000  1        1     5      NA
    2        2000  2        1     4      NA
    3        2000  1        1     5      NA
    3        2000  2        2     7      5

What do you suggest to use for this purpose? Thank you so much.

Marco Mello
  • 175
  • 1
  • 9
  • `library(data.table);setDT(df1)[,newID := paste0("var_",1:.N), by = .(familyid, Year, memberid, count)];dcast(df1, familyid + Year + memberid + count ~ newID, value.var = "var")` – Andre Elrico Oct 17 '18 at 13:01
  • Thank you Andre, this works perfectly. I was missing the first step! Solved. – Marco Mello Oct 17 '18 at 13:09

0 Answers0