0

I have a data frame where one column has some cells where there are two values. I need to split the two contents of those cells into identical rows except the value at said columns contains one of the two values of that cell.

For example:

X.reagent_short_name       X.reagent            VIS
   buffer                    Excipient    732323 // 2343434

Would become:

X.reagent_short_name       X.reagent             VIS
   buffer                    Excipient         732323 
   buffer                    Excipient         2343434

Is there a way to do this and maintain the rest of the dataframe?

Furmole
  • 5
  • 2

2 Answers2

2

With tidyr package, you can use separate_rows():

library(tidyr)
df %>% separate_rows(VIS)

#  X.reagent_short_name X.reagent     VIS
#1               buffer Excipient  732323
#2               buffer Excipient 2343434
Psidom
  • 209,562
  • 33
  • 339
  • 356
0

A hard way with base R:

as.data.frame(
rbind(t(apply(df, 1, function(x) c(x[1:2], unlist(strsplit(x['VIS'], split='//'))[1]))),
rbind(t(apply(df, 1, function(x) c(x[1:2], unlist(strsplit(x['VIS'], split='//'))[2]))))))

X.reagent_short_name X.reagent    VIS1
1               buffer Excipient  732323
2               buffer Excipient 2343434
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63