Extract last word in a string after comma if there are multiple words else the first word
Asked Answered
P

2

10

I have data where the words as follows

 location<- c("xyz, sss, New Zealand", "USA", "Pris,France")
 id<- c(1,2,3)
 df<-data.frame(location,id)

I would like to extract the country name from the data. The tricky part is if i extract just the last word then I will have only one record (France).

library(stringr)
df$country<- word(df$location,-1)

Any ideas on how to extract country data from this data?

 id  location                      country
  1   xyz, sss, New Zealand        New Zealand
  2   USA                          USA
  3   Pris,France                  France
Penholder answered 30/6, 2015 at 21:27 Comment(0)
G
12

You can try sub

 df$country <- sub('.*,\\s*', '', df$location)
 df$country
 #[1] "New Zealand" "USA"         "France"   

Or

 library(stringr)
 str_extract(df$location, '\\b[^,]+$')
 #[1] "New Zealand" "USA"         "France"     
Gerladina answered 30/6, 2015 at 21:28 Comment(1)
explanation [sub]: from df$location, replace any character ., occuring any number of times *, upto a comma, followed by any number/type of whitespace \\s with nothing '' explanation [str_extract]: from df$location, provide 1 or multiple + whole words \\b, not [ ] in a string that ends in a comma ^, until the end of the string $. (so basically, provide all whole words after comma)Citrate
A
1

stringi solution:

require(stringi)
location<- c("xyz, sss, New Zealand", "USA", "Pris,France")
stri_trim(stri_match_first_regex(location, "(^|,)([^,]*?)$")[,3])
## [1] "New Zealand" "USA"         "France"  

stri_trim removes unnecessary spaces before/after country name.

Alixaliza answered 16/1, 2018 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.