I need to write a function that calculates two vectors according to a given matrix BBB1: A - equal to the element product of odd rows of the matrix BBB1 and B – equal to the product of even rows. At the same time, you need to do this without using loops. I understand how to pull out even/odd rows, but I don't understand how to multiply the nth number of rows without a loop.
func <- function(BBB1) {
A <- 1
B <- 1
for (i in (1:dim(BBB1)[1])) {
if (i %% 2 == 0) B <- B*BBB1[i, ]
else A <- A*BBB1[i, ]
}
m <- list(A, B)
return(m)
}
So I wrote down the function, but only through a loop.
Example:
"V1" "V2" "V3" "V4" "V5"
"1" 1 5 9 13 17
"2" 2 6 10 14 18
"3" 3 7 11 15 19
"4" 4 8 12 16 20
Desired result:
Vector AA= 3 35 99 195 323
Vector BB= 8 48 120 224 360