I want to make a loop that uses a different variable each time the loop repeats. Let's say I have 3 different variables: x, y, and z:
x <- 1
y <- 2
z <- 3
I want to perform the same calculation on x, y, and z. I want to multiply by 2:
B <- A*2
Before each loop, I want A to be set equal to the new variable. The long way to do this would be to write it all out manually:
A <- x
B <- A*2
A <- y
B <- A*2
A <- z
B <- A*2
I want to the result to be a vector of this form.
C <- c(2,4,6)
My calculation and variables are much longer than the ones I've shown here, so writing it out manually would make my code very messy. I would prefer to use a loop so I only have to write the calculation once.
help("mapply")as inmapply('*', list(x, y, z), list(2)).