Home > OS >  Using 'apply' and 'recode' together
Using 'apply' and 'recode' together

Time:11-27

I need to recreate some variables but instead of using a for loop I am trying to use 'apply' functions with the 'recode' function.

Here is some of the code I've tried so far...

vars <- c("var", "var2", "var3", "var4", "var5")

recode_vars <- apply(recode(mydata[, vars], "lo:129=0; 130:hi=1"))

However, this produces the error message: Error in match.fun(FUN) : argument "FUN" is missing, with no default.

Does anyone know how I could resolve this? Or just how to use 'apply' and 'recode' together in general?

CodePudding user response:

We may use recode with across

library(dplyr)
vars <- c("var1", "var2", "var3", "var4", "var5")
mydata %>%
   mutate(across(all_of(vars), ~ recode(., "lo:129" = 0, "130:hi" = 1)))

-output

   var1 var2 var3 var4 var5
1    0    1    1    1    1
2    0    0    1    0    0
3    1    0    1    1    0
4    0    1    0    0    1

Or if we need to use apply, specify the MARGIN to loop across the columns - 2

mydata[vars] <- apply(mydata[vars], MARGIN = 2, 
     function(x) recode(x, "lo:129" = 0, "130:hi" = 1))

data

set.seed(24)
mydata <- as.data.frame(matrix(sample(c("lo:129", "130:hi"), 4 * 5, 
   replace = TRUE), ncol = 5, dimnames = list(NULL, paste0("var", 1:5))))

CodePudding user response:

Recode maps specific values to other specific values, when you seem to want retain values between 129 and 130 and flag all the others as low(0) or high(1). It is hard to tell from your example, because it is uncertain if the inputs are integer. You can easily do this using if-then or case_when logic. Here is a quick solution to get you going.

library(purrr)
df <- data.frame(var1=runif(10, min = 128, max = 131), 
                 var2=runif(10, min = 128, max = 131))

n  <- df %>% map_dfc(~ case_when(. <= 129 ~ 0, . >= 130 ~ 1, T ~ .))

if the inputs are all integer, you can simply remove the last clause in the case_when statement as it will never be true.

  •  Tags:  
  • r
  • Related