JNB
JNB

Reputation: 171

How to apply a function to certain columns while keeping the entire data frame?

I am trying to assign NA to certain columns in a data frame when they equal 33.

df$A[df$A == 33] <- NA
df$B[df$B == 33] <- NA
df$C[df$C == 33] <- NA

...and so on.

I ran below codes

as.data.frame(apply(df[,c('A','B','C','D',...etc)], MARGIN=2, function(x) {ifelse(x==33, NA, x)}))

but this leaves me a data frame consists of only those specified columns. (I try to avoid cbind because it gets complicated when I actually need to do multiple cbind/rbind/merge at any step) Is there a simpler way of doing it?

Upvotes: 0

Views: 50

Answers (2)

Kent Johnson
Kent Johnson

Reputation: 3388

Tidyverse version is something like

df = df %>% dplyr::mutate_at(c('A', 'B', 'C'), tidyr::na_if, 33)

Upvotes: 2

akrun
akrun

Reputation: 887118

Here is an option, specify the columns of interest in a vector ('nm1') and assign the subset of columns where that subset is equal to 33 with NA

nm1 <- c("A", "B", "C")
df[nm1][df[nm1]==33] <- NA

Or with is.na

is.na(df[nm1]) <- df[nm1] == 33

Upvotes: 0

Related Questions