mkrasmus mkrasmus - 8 days ago 5
R Question

apply a function across numbered variables in r

Given the following dataframe ('data'):

data <- data.frame(a1 = sample(10), b1 = rep(0,10), c1 = sample(10),
a2 = rep(0,10), b2 = sample(10), c2 = sample(10),
a3 = sample(10), b3 = sample(10), c3 = sample(10))


And a function I created to make calculations dependent on 3 conditions:

calcs <- function(a, b, c){
if ((max(a) > 0) & (max(b)>0)){
maxa <- max(a)
maxb <- max(b)
maxc <- max(c)
calc = 75*c/maxc + 12.5*a/maxa + 12.5*b/maxb
}
else if ((max(a) > 0) & (max(b)==0)){
maxa <- max(a)
maxc <- max(c)
calc = 75*c/maxc + 25*a/maxa
}

else {
maxc <- max(c)
maxb <- max(b)
calc = 75*c/maxc + 25*b/maxb
}
}


I would like to apply this function to the dataframe, looping over the numbered variables (from 1 to 3) instead of using the following code:

data$calc1 <- calcs(data$a1,data$b1,data$c1)
data$calc2 <- calcs(data$a2,data$b2,data$c2)
data$calc3 <- calcs(data$a3,data$b3,data$c3)


I've looked elsewhere, obviously a simple problem, but with the variety of 'tools' documentation on lists, lapply etc I am not sure the best course of action.

Answer

We can use Map with inputs of 'a', 'b', and 'c' subset of columns as input.

data[paste0("calc", 1:3)] <- Map(calcs, 
          data[paste0("a", 1:3)], data[paste0("b", 1:3)], data[paste0("c", 1:3)])