Hi,
I have this simple data file:
data.frame(stringsAsFactors=FALSE,
Unique.respondent.number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
comment = c("I have seen many various charges in my life,
but I don’t like your saving rates",
"I like R Studio", "No comment",
"Main benefit is having low charges",
NA,
"Charge could be an issue",
"Issues with saving rates", "Good saving rates",
"Many benefits like reasonable charges", "N/A", "-",
"Nothing")
)
Now, I have to create new variables based on key words found in the "comment" str variable.
My R guru ( andresrcs) helped me to create this code:
source$Blank <- ifelse(is.na(source$comment), 1, ifelse(str_length(source$comment)<3, 1, 0))
library(dplyr)
library(stringr)
negative_sentiments <- regex("don.?t\\slike|issue|bad", ignore_case = TRUE)
result <- source %>%
mutate(Charges_Fees = if_else(str_detect(comment, regex("charges?", ignore_case = TRUE)) &
!str_detect(comment, regex("benefits?", ignore_case = TRUE)), 1, 0),
Poor_Rates = if_else(str_detect(comment, negative_sentiments) &
str_detect(comment, regex("saving\\srates", ignore_case = TRUE)), 1, 0)) %>%
mutate_if(is.numeric, ~if_else(is.na(.), 0, .))
result
Once, the coding is done I need to create a new variable called "Other" if none of the new variables above is equal one.
I know there are many ways of doing that but I am looking for a clever solution as the source data file is growing and I will have to create more and more variables in the code above.
Can I set up something like:
If Blank+Charges_Fees+Poor_Rates+"variable4"+"variable4"+...+"variable_n"=0 then "Other"=1?