提问人:Hack-R 提问时间:7/13/2016 最后编辑:Hack-R 更新时间:7/1/2018 访问量:145
循环中的因子水平无效,无法对“strsplit”数据进行
Invalid factor level in loop to `strsplit` data
问:
示例数据:
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"
答:
2赞
RHertel
7/13/2016
#1
在这种情况下,似乎不需要循环。你可以试试
dat1 <- do.call(rbind, strsplit(tmp, ","))
评论
str(dat1)
for
do.call(rbind,lapply(tmp,function(x) strsplit(x,",")[[1]]))
dat1
dat1 <- do.call(rbind,strsplit(tmp,","))