So I have a dataframe that looks like this:
df <- data.frame(ID = c("A","A","A","A","A","B","B","B","B","B","C","C","C","C","C"),
code = c(1,2,2,3,3,1,2,2,1,1,3,2,2,1,1),
class = c(2,4,5,5,2,3,2,5,1,2,4,5,3,2,1),
n = c(10,10,20,15,25,20,10,10,20,25,10,15,10,15,15))
I want to transpose it, so that the column class
becomes rows, and n
is spread based on ID
, class
and n
. So it would look like this:
|ID|class|1 |2 |3 |4 |5 |
|A |1 | |10| | | |
|A |2 | | | |10|20|
|A |3 | |25| | |15|
....