Home > Enterprise >  reference a fixed column in mutate_at / mutate_all / mutate_if
reference a fixed column in mutate_at / mutate_all / mutate_if

Time:09-23

Was trying to do a simple task: for all character columns in the dataset, if the column referenced is "#NA#" and the age column is above 14 I want to transform "#NA#" into 999, so my dataframe looks like this:

age   var1     var2      num_var
20    "#NA#"  "hello"    35
30    "bye"  "#NA#"      32
10    "hi"  "#NA#"       35

And want to transform it into this:

age   var1     var2      num_var
20    999  "hello"     35
30    "bye"  999       32
10    "hi"   "#NA#"    35

(Could also be "999" instead of 999)

Tried something like this:

replace_NA <- function(x){
  if_else(x=="#NA#" & age>14, 999, x)}

df %>%  mutate_if(is.character, replace_NA))

But I'm getting a object 'age' not found error. What can be done in a case like this?

Thanks

CodePudding user response:

We can change the function to have 'age' as input argument. In addition, change 'yes' to return a character element instead of numeric i.e. "999" as if_else is type specific compared to ifelse

replace_NA <- function(x, age){
  if_else(x=="#NA#" & {{age}}>14, "999", x)
}

and test it as

library(dplyr)
df %>% 
    mutate(across(where(is.character), replace_NA, age = age))

-output

   age var1  var2 num_var
1  20  999 hello      35
2  30  bye   999      32
3  10   hi  #NA#      35

data

df <- structure(list(age = c(20L, 30L, 10L), var1 = c("#NA#", "bye", 
"hi"), var2 = c("hello", "#NA#", "#NA#"), num_var = c(35L, 32L, 
35L)), class = "data.frame", row.names = c(NA, -3L))
  • Related