Hi.
I have a dataset like the following and I'm trying to add value to 3 columns based on column Country.
Country<-c("Asia","Africa - Benin (Cotonou)",
"Europe - France (Paris)","Asia - China(Shanghai)", "Europe - United Kingdom (London)", "Europe - France (Orléans)"
, "Afrique - Togo (Lomé)", "Afrique - Sénégal (Dakar)")
ID<-c(1,2,3,4,5,6,7,8)
mydata<-data.frame(ID,Country)
> mydata
> ID Country col1 col2 col3
> 1 1 Asia
> 2 2 Africa - Benin (Cotonou)
> 3 3 Europe - France (Paris)
> 4 4 Asia - China(Shanghai)
> 5 5 Europe - United Kingdom (London)
> 6 6 Europe - France (Orléans)
> 7 7 Afrique - Togo (Lomé)
> 8 8 Afrique - Sénégal (Dakar)
I tried the following but im having issue with the regular expression
> mydata[c("Col1", "col2", "col3")] <- str_split_fixed(mydata$Country, '-d' , 3)
I want the result to be like the following:
> mydata
> ID Country Col1 Col2 Col3
> 1 1 Asia Asia <NA> <NA>
> 2 2 Africa - Benin (Cotonou) Africa Benin Cotonou
> 3 3 Europe - France (Paris) Europe France Paris
> 4 4 Asia - China(Shanghai) Asia China Shanghai
> 5 5 Europe - United Kingdom (London) Europe United Kingdom London
> 6 6 Europe - France (Orléans) Europe France Orléans
> 7 7 Afrique - Togo (Lomé) Afrique Togo Lomé
> 8 8 Afrique - Sénégal (Dakar) Afrique Sénégal Dakar
Any suggestion on how to do this?
Thanks in advance