0

I'm trying to extract semicolon separated values from each row for a particular column in a data frame. See table below

ID       Collab                  City    
1        New York;NY;US
2        San Francisco;CA;US
3        Stockholm;Stockholm;SE

I want to be able to extract each value in the Collab column using the semicolon as the separator. I used a for loop to loop through the data frame and trying to update the city and the state columns using the extract strings from the Collab columns. I tried the code below

 "dframe" is the name my data frame
 #loop code begings

  for (row in 1:nrow(dframe)) {
   splitText <- as.character (dframe[row,"Collab"])
  splitFinal<-strsplit(splitText, ";")
 #update column city for that row with extracted value
  dframe[row, "City"]<-splitFinal[1]
 }  

I'm trying to access the position of each string and use it to update the respective column. If there is any post the solves this exact problem, please point to me and I will delete this. I searched extensively but found now answer. Thank you in advance.

Bayuo Blaise
  • 237
  • 3
  • 9
  • `library(tidyverse); df %>% separate(Collab, into = c("City", "State", "Country"), sep = ";") ` – Ronak Shah Nov 12 '18 at 10:34
  • Well, I just need to be able to extract any position of the extracted text. e.g I was trying to extract only the city and update but got the error below:
    Expected 1 pieces. Additional pieces discarded in 122 rows [10, 12, 13, 15, 17, 31, 37, 38, 40, 42, 52, 56, 63, 69, 71, 76, 82, 89, 90, 95, ...]. And the code did not update
    – Bayuo Blaise Nov 12 '18 at 11:05
  • @Ronak, how to i access the position of one character like only City? I don't need the rest because they were just to show as an example – Bayuo Blaise Nov 12 '18 at 11:28
  • you can just delete all other columns then which you don't need. – Ronak Shah Nov 12 '18 at 11:36

0 Answers0