Merging variables containing the same string

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 Unhappyis 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",
          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)

source <- source %>% 
  mutate(Negative = case_when(
    grepl(x., pattern = 'NEG?', = TRUE) ~ 1,
        TRUE ~ 0

but I know that my mutate if completely wrong.
Can you help?

source %>% 

Thank you! Silly question. What do I need to change to have 1/0 rather than TRUE/FALSE ?

source %>% 
1 Like

Of course! Thank you :smiley:

I know the issue is resolved but if I wanted to apply it to all records (not just with "NEG"), shall I use something like this?

 mutate(Valid=as.integer(rowSums(.x)>0))  %>% 

the first one should state 1 or 0 if any question answered as 1 and the second one should sum all values

I dont think dplyr know what .x would be in this context, if its related to the pipe, i.e. source, when it would be . as in my example
of course it will fail if its run on non-numeric columns, so you will probably want to use a select in there anyway just with appropriate criteria.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.