0

I am trying to reshape a dataframe in which subsequent cells that have the equal corresponding values on another column should be transitioned into a new dataframe or matrix with them in the same row, but in different cells.

As an example for the mechanism, the dataframe df

df <- data.frame(PATID=c(1,2,2,3,3,3), SUB = c("5A", "2A", "2B", "1A", "2A", "2Y"))

PATID   SUB 
    1    5A 
    2    2A 
    2    2B 
    3    1A 
    3    2A 
    3    2Y

should be transformed into

5A 
2A 2B
1A 2A 2Y

With in this case three rows (= # of unique values in df$PATID) and three columns but this should be scalable.

After searching, I have not found any equivalent question.

Jaap
  • 81,064
  • 34
  • 182
  • 193
user5835099
  • 127
  • 1
  • 8

0 Answers0