I've tried pretty much everything from this similar question, but I can't get the results everyone else seems to be getting. This is my problem:
I have a data frame like this, listing the grades each teacher works with:
> profs <- data.frame(teaches = c("1st", "1st, 2nd",
"2nd, 3rd",
"1st, 2nd, 3rd"))
> profs
teaches
1 1st
2 1st, 2nd
3 2nd, 3rd
4 1st, 2nd, 3rd
I've been looking for solutions to break the teaches
variable into columns, like so:
teaches1st teaches2nd teaches3rd
1 1 0 0
2 1 1 0
3 0 1 1
4 1 1 1
I understand this solution involving the splitstackshape
library and the apparently deprecated concat.split.expanded
function is supposed to do exactly what I want, given the answerer's explanation. However, I can't seem to reach the same results:
> concat.split.expanded(profs, "teaches", fill = 0, drop = TRUE)
Fehler in seq.default(min(vec), max(vec)) :
'from' cannot be NA, NaN or infinite
Using cSplit
, which I understood supersedes "most of the earlier concat.split* functions", I get this:
> cSplit(profs, "teaches")
teaches_1 teaches_2 teaches_3
1: 1st NA NA
2: 1st 2nd NA
3: 2nd 3rd NA
4: 1st 2nd 3rd
I've tried using cSplit
's help and tweaking every one of those parameters, but I just can't get that split. I appreciate any help.
See Question&Answers more detail:
os