-1

I have data that looks as the following:

dt <- data.frame(id = sample(LETTERS, 100) ,A = seq(1:100), B = sample(1:100000, 100), C = rpois(100,1), D = seq(1:100)^3)

I am trying to transform any variable that has a max value > 1000

dt %>% mutate_if(max(.) > 1000, log10)

For model fitting. But I keep getting the following error:

Error in tbl_if_vars(.tbl, .p, .env, ..., .include_group_vars = .include_group_vars) : 
  length(.p) == length(tibble_vars) is not TRUE

Please help!

akash87
  • 3,876
  • 3
  • 14
  • 30

1 Answers1

1

Use a formula argument

library(dplyr)
dt %>% mutate_if(~is.numeric(.) && max(., na.rm = TRUE) > 1000, log10)

To make it more clear you can use an anonymous function call

dt %>% mutate_if(function(x) is.numeric(x) && max(x, na.rm = TRUE) > 1000, log10)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213