循环中的因子水平无效,无法对“strsplit”数据进行

Invalid factor level in loop to `strsplit` data

提问人:Hack-R 提问时间:7/13/2016 最后编辑:Hack-R 更新时间:7/1/2018 访问量:145

问:

示例数据:

tmp <-
c("30.55,114.27", "39.31,115.92", "0,0", "0,0", "27.35,111.78", 
"0,0", "34.47,118.97", "34.62,113.72", "0,0", "0,0", "0,0", "31.43,120.55", 
"0,0", "0,0", "31.67,119.82", "44.28,129.04", "23.37,113.44", 
"23.31,112.84", "24.41,102.34", "30.77,104.24", "0,0")

循环到字符串拆分数据:

dat1 <- data.frame(as.character(NA),as.character(NA))
for(i in 1:length(tmp)){#nrow(train2)){
  dat1[i,] <- do.call(rbind, strsplit(tmp[i], ","))  
}

问题:

有 50 个或更多警告(使用 warnings() 查看前 50 个)

Warning messages:
1: In `[<-.factor`(`*tmp*`, iseq, value = "0") :
  invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, iseq, value = "0") :
  invalid factor level, NA generated
3: In `[<-.factor`(`*tmp*`, iseq, value = "29.29") :
  invalid factor level, NA generated
4: In `[<-.factor`(`*tmp*`, iseq, value = "106.25") :
  invalid factor level, NA generated
5: In `[<-.factor`(`*tmp*`, iseq, value = "0") :
  invalid factor level, NA generated
6: In `[<-.factor`(`*tmp*`, iseq, value = "0") :

但是,如果我只运行它 1 行,它看起来是正确的:

> do.call(rbind, strsplit(tmp[i], ","))  
     [,1]    [,2]    
[1,] "30.55" "114.27"
r

评论

0赞 joran 7/13/2016
在循环之前查看,警告消息可能会更有意义。str(dat1)for
0赞 joran 7/13/2016
...也许你更喜欢更像的东西?do.call(rbind,lapply(tmp,function(x) strsplit(x,",")[[1]]))
0赞 Hack-R 7/13/2016
关于第一条评论,我明白你的意思。虽然我可以发誓,当我设置数字时,我遇到了同样的错误。让我试试你第二条评论中的代码。只需 1 秒。dat1
2赞 RHertel 7/13/2016
循环真的有必要吗?怎么样 ?dat1 <- do.call(rbind,strsplit(tmp,","))
0赞 joran 7/13/2016
@RHertel是的,我认为你是对的。

答:

2赞 RHertel 7/13/2016 #1

在这种情况下,似乎不需要循环。你可以试试

dat1 <- do.call(rbind, strsplit(tmp, ","))