2

I have the following data.frame:

d.f=data.frame(authors=c("Paco Blue; Eddy Michelt","Sara Robert","Anne Reed; Robert A. McDonald; Alice Brice"),title=c("The live is good","Another","Yesterday"))


  authors                                     title
  <fctr>                                      <fctr>
Paco Blue; Eddy Michelt                   The live is good      
Sara Robert                                  Another        
Anne Reed; Robert A. McDonald; Alice Brice  Yesterday

I would like to split the authors field by “;” , that is, get the following data.frame from the previous one:

d.f1<-data.frame(authors=c("Paco Blue","Eddy Michelt","Sara Robert","Anne Reed","Robert A. McDonald","Alice Brice"), title=c("The live is good","The live is good","Another","Yesterday","Yesterday","Yesterday"))
d.f1
 authors                 title
 <fctr>                 <fctr>
 Paco Blue         The live is good         
 Eddy Michelt      The live is good         
 Sara Robert           Another          
 Anne Reed           Yesterday          
 Robert A. McDonald  Yesterday          
 Alice Brice         Yesterday

Thanks in advance.

nilrem
  • 85
  • 5

2 Answers2

1

This is done so often, there is a function for it:

library(tidyr)
separate_rows(df, authors, sep=';')
F Trias
  • 189
  • 8
0

An option with cSplit

library(splitstackshape)
cSplit(d.f, 'authors', sep="; ", 'long', fixed = FALSE)
#                authors            title
#1:            Paco Blue The live is good
#2:         Eddy Michelt The live is good
#3:          Sara Robert          Another
#4:            Anne Reed        Yesterday
#5:   Robert A. McDonald        Yesterday
#6:         *Alice Brice        Yesterday
akrun
  • 874,273
  • 37
  • 540
  • 662