循环到`strsplit`数据

时间:2016-07-13 14:27:05

标签: r

示例数据:

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个或更多警告(使用警告()查看前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") :

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

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

1 个答案:

答案 0 :(得分:2)

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

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