警告:结果列数不是向量长度的倍数(arg 1)

Ray*_* Sp 3 r

我想分裂一个专栏.我的数据框(menuD)就像

Column1
1|3|4|5
4|5|6|7
Run Code Online (Sandbox Code Playgroud)

我想在column1中拆分数字,所以我这样做了.

menuD <- data.frame (do.call('rbind', strsplit(as.character(myCmenu$myFile.menudata), '|', fixed = TRUE)))
Run Code Online (Sandbox Code Playgroud)

我得到了这样的预期结果

col1 | col2 | col3 | col4
  1  |  3   |  4   | 5
  4  |  5   |  6   | 7
Run Code Online (Sandbox Code Playgroud)

但我收到了来自R的警告信息

> Warning message:
In rbind(c("", "164200", "", "167", "108", "112", "116", "120"),  :
  number of columns of result is not a multiple of vector length (arg 1)
Run Code Online (Sandbox Code Playgroud)

我想知道,这对我的数据有影响吗?所有数据是否正确分开?

akr*_*run 6

根据提供的示例,或者 cSplit

library(splitstackshape)
cSplit(menuD, "Column1", "|")
#   Column1_1 Column1_2 Column1_3 Column1_4
#1:         1         3         4         5
#2:         4         5         6         7
Run Code Online (Sandbox Code Playgroud)

separate来自tidyr

library(tidyr)
separate(menuD, Column1, into = paste0("col", 1:4))
Run Code Online (Sandbox Code Playgroud)

或者 read.table/read.csv可以使用.

read.table(text=menuD$Column1, sep="|", fill=TRUE, header=FALSE)
Run Code Online (Sandbox Code Playgroud)

但是,OP的帖子中的警告表明"Column1"中可能有少量的元素|.在这种情况下,cSplit或最后一个选项应该工作.