Hi,
I have a df including some variables which indicate positive and negative behavior. Negative variables have "NEG" in their names.
How can I create a variable called "Negative" where: If any variable containing "NEG" is 1 then the value of the Negative is one, otherwise is 0. In this example if NEG - Long/Slow
is 1 or NEG Unhappy
is 1 then "Negative" is 1. As a result first and last 3 URNs should be positive ("Negative"=0) and other 5 URNs should be negative ("Negative"=1).
I have done this:
source <- data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
URN = c("MA70573","MA70606","MA70669","MA70556",
"MA70626","MA70641","MA70592",
"MA70668","MA70515"),
Efficient = c(1, 0, 0, 0, 0, 0, 1, 0, 0),
`NEG - Long/Slow` = c(0, 0, 1, 1, 1, 0, 0, 0, 0),
`NEG Unhappy` = c(0, 1, 0, 0, 0, 1, 0, 0, 0),
Easy = c(0, 1, 0, 0, 1, 0, 0, 0, 1)
)
library(dplyr)
source <- source %>%
mutate(Negative = case_when(
grepl(x., pattern = 'NEG?', ignore.case = TRUE) ~ 1,
TRUE ~ 0
))
but I know that my mutate if completely wrong.
Can you help?