splitstackshape pkg - concat.split.expanded 通过强制错误返回 NA

splitstackshape pkg - concat.split.expanded returning NA by coercion errors

我按照此处的说明 Dummy variables from a string variable 尝试将一列字符串(由空格分隔的单词)转换为虚拟变量(0-1 表示单词 notused/used 在该行中的字符串)使用 concat.split.expanded 但会出现以下错误:

In lapply(listOfValues, as.integer) : NAs introduced by coercion

前面有

之一
Error in seq.default(min(vec), max(vec)) : 'from' cannot be NA, NaN or infinite

我很确定要转换的列中没有任何 NA,更不用说那么多了。不知道如何解决这个问题。谢谢!

命令我已经运行产生了问题:

concat.split.expanded(dataset, "stringvarname", sep = " ", mode = "binary", drop = false)

有无填充都会产生问题=

您需要指定要拆分连接的字符串(下面示例数据中的 "var2"),而不是将数值连接成字符串(下面示例数据中的 "var3")。

这是一个重现您的错误并显示有效解决方案的示例:

df = data.frame(var1 = 1:2, var2 = c("a b c", "a c d"), var3 = c("1 2 3", "1 2 5"))
library(splitstackshape)

cSplit_e(df, "var3", sep = " ")
#   var1  var2  var3 var3_1 var3_2 var3_3 var3_4 var3_5
# 1    1 a b c 1 2 3      1      1      1     NA     NA
# 2    2 a c d 1 2 5      1      1     NA     NA      1

## Will give you an error
cSplit_e(df, "var2", sep = " ")
#  Error in seq.default(min(vec), max(vec)) : 
#   'from' cannot be NA, NaN or infinite In addition: Warning messages:
# 1: In lapply(listOfValues, as.integer) : NAs introduced by coercion
# 2: In lapply(listOfValues, as.integer) : NAs introduced by coercion

cSplit_e(df, "var2", sep = " ", type = "character")
#   var1  var2  var3 var2_a var2_b var2_c var2_d
# 1    1 a b c 1 2 3      1      1      1     NA
# 2    2 a c d 1 2 5      1     NA      1      1

为什么? cSplit_e使用seqseq用于数字输入。

> seq("a", "c")
Error in seq.default("a", "c") : 'from' cannot be NA, NaN or infinite