Reputation: 1020
I am trying to split a column. my data frame (menuD) is like
Column1
1|3|4|5
4|5|6|7
I wanted to split the numbers in column1 so I did this.
menuD <- data.frame (do.call('rbind', strsplit(as.character(myCmenu$myFile.menudata), '|', fixed = TRUE)))
I got the expected result which is like this
col1 | col2 | col3 | col4
1 | 3 | 4 | 5
4 | 5 | 6 | 7
But I got this warning message from 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)
I'd like to know, did this effect my data? is all the data separated properly?
Upvotes: 4
Views: 29705
Reputation: 886938
Based on the example provided, either cSplit
library(splitstackshape)
cSplit(menuD, "Column1", "|")
# Column1_1 Column1_2 Column1_3 Column1_4
#1: 1 3 4 5
#2: 4 5 6 7
Or separate
from tidyr
library(tidyr)
separate(menuD, Column1, into = paste0("col", 1:4))
Or read.table/read.csv
can be used.
read.table(text=menuD$Column1, sep="|", fill=TRUE, header=FALSE)
But, the warning in the OP's post indicates that there might be elements in the "Column1" with less number of |
. In that case, the cSplit
or the last option should work.
Upvotes: 6