'How to convert some values to negative in a column

my_df <- df %>%
mutate(incExpChange= case_when(
(incNextYear == 'Higher' & INEXQ2 %in% 0:95) ~ INEXQ2,
(incNextYear == 'Lower' & INEXQ2 %in% 0:95) ~ -INEXQ2))

I have two columns 'incNextYear' & 'INEXQ2', I want to convert some positive values in 'INEXQ2' to negative in my new column when incNextYear is 'Lower'. -INEXQ2 is not working for me. What am I doing wrong?

 structure(list(rID = 1:6, region = structure(c(1L, 2L, 3L, 4L, 1L, 4L), .Label = c("West", "Northeast", "South", "Midwest"), class = "factor"), incNextYear = structure(c(1L, 1L, 1L, 2L, 1L, 2L), .Label = c("Higher", "Lower", "About the same", "DK"), class = "factor"), homeBuying = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("BAD", "GOOD", "PRO-CON"), class = "factor"), REGION = c("West", "Northeast", "South", "Midwest", "West", "Midwest"), INEXQ1 = c("Higher", "Higher", "Higher", "Lower", "Higher", "Lower"), INEXQ2 = c("2", "3", "13", "3", "6", "5"), V204 = c("BAD", "BAD", "BAD", "BAD", "BAD", "BAD")), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")) 


Solution 1:[1]

Replace

-INEXQ2 

with

INEXQ2*(-1)

Replace

(incNextYear == 'Higher' & INEXQ2 %in% 0:95)

with

((incNextYear == 'Higher') && (INEXQ2 %in% 0:95))

another expression is also changed like above.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1