I have a df that looks as follows:
id name grade
1 rich, tom, todd, 12
2 chris,mary 9
3 larry 10
I run the following code to split text to column:
newdf <- within(df, name<-data.frame(do.call('rbind', strsplit(as.character(name), ',', fixed=TRUE))))
And here is my output:
id name.X1 name.X2 name.X3 grade
1 rich tom todd 12
2 chris mary chris 9
3 larry larry larry 10
The code I have is repeating names(in id 2 & 3), as opposed to putting in blanks or NA. What I'd like the code to output is the following:
id name.X1 name.X2 name.X3 grade
1 rich tom todd 12
2 chris mary N/A 9
3 larry N/A N/A 10
Or instead of N/A I'd like for the cells to be left blank. Any idea how I can avoid having it repeat names? Thank you.