您需要指定要拆分连接的字符串(下面的示例数据中的“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
使用seq
, 和seq
用于数字输入。
> seq("a", "c")
Error in seq.default("a", "c") : 'from' cannot be NA, NaN or infinite