Home > OS >  How to change any row with certain values to NA across multiple columns?
How to change any row with certain values to NA across multiple columns?

Time:08-17

Assume there is a data frame with many variables. Values of 9 in columns 21:31 need to be changed to NA. NA values in existing columns (21:31) need to remain NA. Values equal or less than 7 need to remain their current value (21:31). Outside the columns mentioned, data needs to remain the same.

In this example 21:31 are the numbers of the columns, not the variable names.

E.g

ID  21   22   23    24 
1   3    5    3     9
2   NA   NA   NA    NA
3   5    7    7     7
4   9    9    9     9

I would be very grateful for help with this - I would / am attempting to do it myself, but I am in the midst of writing my thesis and am pretty new to R and it is taking a lot longer than I would like it to.

Thanks in advance.

CodePudding user response:

In base R, subset the data other than the first column ('ID'), create a logical expression to match 9 value and assign those NA

df1[-1][df1[-1]==9] <- NA

CodePudding user response:

In dplyr, you can use na_if across the selected columns:

library(dplyr)
df %>% 
   mutate(across(21:31, ~ na_if(.x, 9)))
  • Related