product of first n elements of vector

Viewed 24

I have problem to find first n elements of vector in R.

I have tried prod() function, but question is how first n elements?

How to find product of first n elements of vector in R?

1 Answers

An idea would be to create a function where you determine vector entry and length of desired elements for product formula

# Function
prodFun <- function(x, len){
  return(prod(x[1:len]))
}

# Example
x <- 1:10
prodFun(x, 5)
Related