我已经尝试了这个类似问题的几乎所有内容,但我无法得到其他人似乎得到的结果。这是我的问题:
我有一个这样的数据框,列出了每个老师的成绩:
> 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
我一直在寻找将teaches
变量分成列的解决方案,如下所示:
teaches1st teaches2nd teaches3rd
1 1 0 0
2 1 1 0
3 0 1 1
4 1 1 1
我理解这个涉及splitstackshape
库的解决方案,鉴于回答者的解释,显然已弃用的concat.split.expanded
函数应该完全符合我的要求。但是,我似乎无法达到相同的结果:
> concat.split.expanded(profs, "teaches", fill = 0, drop = TRUE)
Fehler in seq.default(min(vec), max(vec)) :
'from' cannot be NA, NaN or infinite
使用cSplit
,我理解它取代了“大多数早期的 concat.split* 函数”,我得到了这个:
> 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
我已经尝试使用cSplit
's help 并调整每一个参数,但我就是无法拆分。我很感激任何帮助。