I have a column that contains thousands of descriptions like this (example) :
| Description |
|---|
| Building a hospital in the city of LA, USA |
| Building a school in the city of NYC, USA |
| Building shops in the city of Chicago, USA |
I'd like to create a column with the first word after "city of", like that :
| Description | City |
|---|---|
| Building a hospital in the city of LA, USA | LA |
| Building a school in the city of NYC, USA | NYC |
| Building shops in the city of Chicago, USA | Chicago |
I tried with the following code after seeing this topic Extracting string after specific word, but my column is only filled with missing values
library(stringr)
df$city <- data.frame(str_extract(df$Description, "(?<=city of:\\s)[^;]+"))
df$city <- data.frame(str_extract(df$Description, "(?<=of:\\s)[^;]+"))
I took a look at the dput() and the output is the same than the descriptions i see in the dataframe directly.