简体   繁体   中英

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

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM