The initial data frame mergedDf
is
PROD_CODE
1 PRD0900033,PRD0900135,PRD0900220,PRD0900709
2 PRD0900097,PRD0900550
3 PRD0900121
4 PRD0900353
5 PRD0900547,PRD0900614
After calling
mergedDf<-data.frame(do.call('rbind', strsplit(as.character(mergedDf$PROD_CODE),',',fixed=TRUE)))
Output becomes
X1 X2 X3 X4
1 PRD0900033 PRD0900135 PRD0900220 PRD0900709
2 PRD0900097 PRD0900550 PRD0900097 PRD0900550
3 PRD0900121 PRD0900121 PRD0900121 PRD0900121
4 PRD0900353 PRD0900353 PRD0900353 PRD0900353
5 PRD0900547 PRD0900614 PRD0900547 PRD0900614
It seems the excess rows are being refilled.
I have tries using bind_rows()
, rbind_all()
but these require changing the splitted ones to data.frame which I couldn't do. I also tried using rbindlist()
which also needed a data.frame as an argument.
I need the ouput to be. The positions do not matter.
X1 X2 X3 X4
1 PRD0900033 PRD0900135 PRD0900220 PRD0900709
2 PRD0900097 PRD0900550 NA NA
3 PRD0900121 NA NA NA
4 PRD0900353 NA NA NA
5 PRD0900547 PRD0900614 NA NA
Or if anyone could recommend a nicer way to format for apriori algorithm implementation it would be nice. Please Help.
You can try
cSplit
Or using the devel version of
data.table
i.e.v1.9.5
Or using
stringi
(contributed by @David Arenburg)Or
separate
fromtidyr
(contributed by @David Arenburg)Or using
base R
Or with
strsplit
(lengths
function was introduced inR 3.2.0
. The equivalent code for earlier versions issapply(lst, length)
)