I have a vector [1:360] with integers and need to find the products of the first, second ... twelfth set of 30 elements. Ultimately, I need a function that gives me a vector [1:12] with the products of all twelve 30-element intervals. I'm fairly new to R and have been stuck on this for too long.
A simple way to do this would be to turn your vector into a 30-row matrix and get the product of each column.
In the absence of a reproducible example, let's make one with a vector of 360 numbers drawn from a normal distribution:
set.seed(69)
vec <- rnorm(360)
We can turn vec
into a 30 * 12 matrix by just doing matrix(vec, nrow = 30)
, which will fill the matrix by column. We then get the product of each column by using apply
to apply the function prob
to each column.
apply(matrix(vec, nrow = 30), 2, prod)
#> [1] -6.253460e-09 -4.413086e-09 -1.332389e-10 1.041448e-08 -1.779489e-08 1.255979e-10
#> [7] 3.463687e-13 -6.265196e-12 8.300651e-04 -1.041469e-10 4.256378e-09 1.439522e-09