library(data.table)
dat1 <- data.table(id = c(1, 2, 34, 99),
class = c("sports", "", "music, sports", ""),
hobby = c("knitting, music, sports", "", "", "music"))
> dat1
id class hobby
1 1 sports knitting, music, sports
2 2
3 34 music, sports
4 99 music
I have the above dataset, dat1, where each row corresponds to a unique id. For each id, multiple inputs for either class or hobby are separated by a comma.
I would like to exchange the row and column of this dataset so that I get the following:
input class hobby
1 sports 1, 34 1
2 knitting 1
3 music 34 1, 99
In this dataset, each row corresponds to a unique input from dat1. Now the class and hobby columns are storing the corresponding ids from dat1, each separated by a comma.
Is there a quick way to swap the row and columns like this in R?