I wanted to follow up on the question that I posted here. While I received baseR and data.table solution, I was trying to implement the same using cSplit_e from splitstackshape package as suggested in the comment of my previous post. With the modified data as below (i.e. with NA),
data1<-structure(list(reason = c("1", "1", NA, "1", "1", "4 5", "1",
"1", "1", "1", "1", "1 2 3 4", "1 2 5", NA, NA)), .Names = "reason", class = "data.frame", row.names = c(NA,
-15L))
#loading packages
library(data.table)
library(splitstackshape)
cSplit_e(setDT(data1),1," ",mode = "value") # with NA's doesn't work
Error in seq.default(min(vec), max(vec)) : 'from' must be a finite number
data2<-na.omit(setDT(data1),cols="reason") # removing NA's
cSplit_e(data2,1," ",mode = "value") # without NA's works
reason reason_1 reason_2 reason_3 reason_4 reason_5
1: 1 1 NA NA NA NA
2: 1 1 NA NA NA NA
3: 1 1 NA NA NA NA
4: 1 1 NA NA NA NA
5: 4 5 NA NA NA 4 5
6: 1 1 NA NA NA NA
7: 1 1 NA NA NA NA
8: 1 1 NA NA NA NA
9: 1 1 NA NA NA NA
10: 1 1 NA NA NA NA
11: 1 2 3 4 1 2 3 4 NA
12: 1 2 5 1 2 NA NA 5
So, the question is does cSplit_e account for NA's in column to be splited?
NAs in the column to be split. Do you mind filing a bug-report at the GitHub page for the package and I'll look into it? – A5C1D2H2I1M1N2O1R2T1 Feb 2 '18 at 7:20splitstackshape:::numMat()which takes basically the same approach that akrun has taken in his base R approach. If you try his approach with a column that includesNAvalues, you'll also encounter an error. The function was designed keeping integers in mind, but did not account forNAvalues. Should be fixable using a combination ofna.rmandna.omitin various places. – A5C1D2H2I1M1N2O1R2T1 Feb 2 '18 at 7:37setDT(data1)[, strsplit(reason," "), by = .(rn = factor(seq_len(nrow(data1))))] %>% na.omit(.) %>% dcast(., rn ~ V1, drop = FALSE). – A5C1D2H2I1M1N2O1R2T1 Feb 2 '18 at 9:01