I am trying to understand the mapply function, but I don't get it. Lets suppose I want to multiply each element of a vector with each element of another vector like this:
a <- c(1,2)
b <- c(1,2,3)
for (i in a){
for (j in b){
print(i*j)
}
}
Return 1 2 3 2 4 6, thats what I want.
func <- function(x,y){
return (x*y)
}
mapply(func, a, b)
mapply does not generate all the combinations of a and b. It simply applies over the vectors a and b, recycling them if necessary. So in your case a become c(1,2,1) to make it the same length as b. Read the documentation for mapply.