我已经尝试了几乎所有的从this similar question,但我不能得到其他人似乎正在取得的结果。这是我的问题:
我有一个这样的数据框架,列出了每一位教师的工作成绩:
> 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函数的splitstackshape应该做我想做的事情。然而,我似乎不能达到同样的结果:
> 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的帮助并调整其中的每一个参数,但我就是无法得到这种分割,我很感谢任何帮助。
发布于 2015-03-17 17:39:21
由于您的连接数据是连接字符串(而不是连接的数值),因此需要添加type = "character"才能使函数按您的预期工作。
函数的默认设置是数值,因此出现了关于NaN等的错误。
命名与同一家族中其他功能的简短形式更加一致。因此,现在是cSplit_e (尽管旧的函数名仍然有效)。
library(splitstackshape)
cSplit_e(profs, "teaches", ",", type = "character", fill = 0)
# teaches teaches_1st teaches_2nd teaches_3rd
# 1 1st 1 0 0
# 2 1st, 2nd 1 1 0
# 3 2nd, 3rd 0 1 1
# 4 1st, 2nd, 3rd 1 1 1?concat.split.expanded的帮助页面与cSplit_e的帮助页面相同。如果你有任何技巧让它更清楚地理解,请提出一个问题,在包的GitHub页面。
发布于 2015-03-17 14:37:24
这是另一种选择:
Vectorize(grepl, 'pattern')(c('1st', '2nd', '3rd'), profs$teaches)
# 1st 2nd 3rd
# [1,] TRUE FALSE FALSE
# [2,] TRUE TRUE FALSE
# [3,] FALSE TRUE TRUE
# [4,] TRUE TRUE TRUE发布于 2015-03-17 14:43:15
你可以试试mtabulate从qdapTools
library(qdapTools)
res <- mtabulate(strsplit(as.character(profs$teaches), ', '))
colnames(res) <- paste0('teaches', colnames(res))
res
# teaches1st teaches2nd teaches3rd
#1 1 0 0
#2 1 1 0
#3 0 1 1
#4 1 1 1或者使用stringi
library(stringi)
(vapply(c('1st', '2nd', '3rd'), stri_detect_fixed, logical(4L),
str=profs$teaches))+0L
# 1st 2nd 3rd
#[1,] 1 0 0
#[2,] 1 1 0
#[3,] 0 1 1
#[4,] 1 1 1https://stackoverflow.com/questions/29101708
复制相似问题