I have data that looks like this:
> head(df)
ID Comment
1 1 I ate dinner.
2 2 We had a three-course meal.
3 3 Brad came to dinner with us.
4 4 He loves fish tacos.
5 5 In the end, we all felt like we ate too much. Code 5.16
6 6 We all agreed; it was a magnificent evening.72 points.
I want to create two new columns, one called A
and one called B
.
I want column A to equal 1 if any of the following words/phrases occur: dinner,evening,we ate
I want column B to equal 1 if any of the following words/phrases occur: in the end,all,Brad,5.16
.
How can I go about doing this? Mind you I need an exact match.
Does this work:
library(dplyr)
library(stringr)
df %>% mutate(A = +str_detect(Comment,str_c(c('dinner','evening','we ate'), collapse = '|')),
B = +str_detect(Comment,str_c(c('in the end','all','Brad','5.16'), collapse = '|')))
# A tibble: 6 x 4
ID Comment A B
<dbl> <chr> <int> <int>
1 1 I ate dinner. 1 0
2 2 We had a three-course meal. 0 0
3 3 Brad came to dinner with us. 1 1
4 4 He loves fish tacos. 0 0
5 5 In the end, we all felt like we ate too much. Code 5.16 1 1
6 6 We all agreed; it was a magnificent evening.72 points 1 1