我有一个像这样的int列:
idNums
2
101
34
25
8
...
我需要将它们转换为3个因子列,如下所示:
digit1 digit2 digit3
0 0 2
1 0 1
0 3 4
0 2 5
0 0 8
... ... ...
有什么建议吗?
答案 0 :(得分:7)
使用模块算术运算符%%
和%/%
,这是一个有趣的解决方案:
d <- c(2, 101, 34, 25, 8)
res <- data.frame(digit1 = d %/% 100,
digit2 = d %% 100 %/% 10,
digit3 = d %% 10)
# digit1 digit2 digit3
# 1 0 0 2
# 2 1 0 1
# 3 0 3 4
# 4 0 2 5
# 5 0 0 8
请注意,它为每个列返回数字值具有次要但很好的好处。但是,如果您需要 factor 列,请执行以下命令:
res[] <- lapply(res, as.factor)
all(sapply(res, class)=="factor")
#[1] TRUE
答案 1 :(得分:5)
使用formatC
和strsplit
。
idNums <- c(2, 101, 34, 25, 8)
idChars <- formatC(idNums, width = 3, flag = "0")
idChars <- strsplit(idChars, "")
data.frame(
digits1 = sapply(idChars, function(x) x[1]),
digits2 = sapply(idChars, function(x) x[2]),
digits3 = sapply(idChars, function(x) x[3])
)
使用stringr
包更清洁一点。将呼叫替换为strsplit
和
str_split_fixed(idChars, "", 3)
答案 2 :(得分:3)
我认为Richie Cottons使用formatC是kewl所以我把它合并了:
testdat <- read.fwf(textConnection(formatC(idNums, width = 3, flag = "0") ),
widths=c(1,1,1),
col.names=c("digit1", "digit2", "digit3")
)
testdat
#------------
digit1 digit2 digit3
1 0 0 2
2 1 0 1
3 0 3 4
4 0 2 5
5 0 0 8