Although I see several similar issues on stackoverflow regarding this problem I cannot get my syntax to work. I want to split comma separated values into new columns in my dataframe. When I use the following syntax the resulting dataframe doe not make sense:
dat <- data.frame(ID = c(1:10),
var1 = rep(c("A","B"),5),
var2 = c(NA,"100,101,102","105","108,110","106","105,107,109,103","107,106",NA,"101",NA))
dat$var2 = as.character(dat$var2)
splitdat <- do.call(rbind, strsplit(dat$var2, split = ","))
splitdat <- data.frame(apply(splitdat, 2, as.numeric))
The section strsplit(dat$var2, split = ",")
results in a correct list, but I can't add these values as new columns to my df.
Does anyone have the answer?
The desirde output (for the first 4 IDs) would be:
ID var1 var2
1 1 A NA
2 2 B 100
3 2 B 101
4 2 B 102
5 3 A 105
6 4 B 108
7 4 B 110