I am trying to add a column to my data based off of what is contained in a different column within the same data. So just for an example of what i'm looking for:
Column A: "Apple Bees in the right area" "We love Apple Bees here" "Waffle House: the place to go! 5 stars" "Nothing better than Waffle House!"
Note: Column A has value chr
I want Column F to show: "Apple Bees" "Apple Bees" "Waffle House" "Waffle House"
I tried mutate, contains, select, %in%, and I kept getting a bunch of errors. I assume I am supposed to use one or more of these within the dplyr package, but I am not getting the results I want. Any suggestions would be appreciated. Thanks!
Try this:
library(dplyr)
library(stringr)
restaurants <- c("Apple Bees",
"Waffle House") %>%
paste(collapse = "|")
my_df <- data.frame(
matrix(
c(
"Apple Bees in the right area",
"We love Apple Bees here",
"Waffle House: the place to go! 5 stars",
"Nothing better than Waffle House!"
),
nrow = 4,
ncol = 1,
byrow = TRUE,
dimnames = list(NULL,
c("phrase"))
),
stringsAsFactors = FALSE
)
my_df_new <- my_df %>%
mutate(
restaurant_in_phrase = str_extract(phrase, restaurants)
)
If you're doing a lot of work with strings and phrases, I'd highly recommend familiarizing yourself with Regular Expressions or regex.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.