I have some 13 column and 20,000 rows of data. In which one column is having description details. Description column has one or many values separated using delimiters like ",", "-", "/", "&". Need to split the Description column into multiple columns based on different delimiters used. Some values could be just one word and doesn't need a split. Please help me on this.
Here's my piece of code that I tried. But its not working.
df=as.data.frame(openxlsx::read.xlsx("Sample data.xlsx",sheet=1,colNames=TRUE,skipEmptyRows=TRUE))
newdf <- data.frame()
for (i in 1:nrow(df))
{
out <- strsplit(as.character(df$SHORT_DESC[i]),',')
newdf <- rbind(newdf, data.frame(t(sapply(out, `[`))))
}
After this I planned to merge the original dataframe (excluding description) and new dataframe (with description separated in multiple columns), column wise. But my above code fails, as well I wanted to perform this operation in few lines of codes.
INPUT & OUTPUT:
NO SHORT_DESC
1 SEAT, HELICAL, COMPRESSION SPRING
2 RIVET, SOLID, 100 DEGREE COUNTERSUNK HEA
3 WIRE,NICKEL-COPPER,BULK
4 INDUCTOR/ EMI SUPPRESSION
5 CHOKE
6 NOM PLATE & LENS
No SHORT_DESC1 SHORT_DESC2 SHORT_DESC3 SHORT_DESC4
1 SEAT HELICAL COMPRESSION SPRING
2 RIVET SOLID 100 DEGREE COUNTERSUNK HEA
3 WIRE NICKEL COPPER BULK
4 INDUCTOR EMI SUPPRESSION
5 CHOKE
6 NOM PLATE LENS