Split columns in dataframe with NA

  • A+
Category:Languages

I have a df like this:

df <- data.frame(FOO = c('A|B|C', 'A|B', 'B|C', 'A', 'C'))  > df     FOO 1 A|B|C 2   A|B 3   B|C 4     A 5     C 

And I would like to have an output like this:

> df   X1 X2 X3 1 A  B  C 2 A  B 3    B  C 4 A 5       C 

So far I tried with this example: Split column at delimiter in data frame but it is not spliting the columns without repeating values, what I get there is:

df <- data.frame(do.call('rbind', strsplit(as.character(df$FOO),'|',fixed=TRUE)))  > df   X1 X2 X3 1  A  B  C 2  A  B  A 3  B  C  B 4  A  A  A 5  C  C  C 

And I also get this warning:

Warning message: In rbind(c("A", "B", "C"), c("A", "B"), c("B", "C"), "A", "C") : number of columns of result is not a multiple of vector length (arg 2)

What can I do in those cases? Preferably with baseR

 


Simply do:

splt <- strsplit(as.character(df$FOO),"//|") all_val <- sort(unique(unlist(splt))) t(sapply(splt,function(x){all_val[!(all_val %in% x)]<-NA;all_val}))   #     [,1] [,2] [,3] #[1,] "A"  "B"  "C"  #[2,] "A"  "B"  NA   #[3,] NA   "B"  "C"  #[4,] "A"  NA   NA   #[5,] NA   NA   "C"  

data:

df <- data.frame(FOO = c('A|B|C', 'A|B', 'B|C', 'A', 'C')) 

Please note:

My version is base:: (no libraries needed) and general:

It would also work with:

df <- data.frame(FOO = c('A|B|C', 'A|B', 'B|C', 'A', 'C', 'B|D|F')) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: