In a dataframe, I want to create a new column based on the occurence of a specific set of strings (char vector) in another column.
So basically, I want this:
ID Phrases
1 some words
2 some words dec
3 some words nov may
to return this:
ID Phrases MonthsOccur
1 some words NA
2 some words dec dec
3 some words nov may may nov
I have tried the following, and I'm not sure why it's giving me the outcome that it does:
library(dplyr)
vMonths <- c("jan","feb","mar","apr","may","jun","jul","aug","sept","nov","dec")
a <- c(1,2,3)
b <- c('phrase number one', 'phrase dec','phrase nov')
df <- data.frame(a,b)
names(df) <- c("ID","Phrases")
df <- df %>% mutate(MonthsOccur = paste(vMonths[str_detect(Phrases, vMonths)],collapse=" "))
It gives me the following warning:
Warning message: In stri_detect_regex(string, pattern, negate = negate, opts_regex = opts(pattern)) : longer object length is not a multiple of shorter object length
And the following outcome:
ID Phrases MonthsOccur
1 some words dec
2 some words dec dec
3 some words nov may dec
tolower(month.abb)
- zx8754