Menu
Newbedev LogoNEWBEDEV Python Javascript Linux Cheat sheet
Newbedev LogoNEWBEDEV
  • Python 1
  • Javascript
  • Linux
  • Cheat sheet
  • Contact

How to cumulatively multiply vector without using cumprod in r?

One option could be:

Reduce(`*`, x, accumulate = TRUE)

[1] 1 2 6

It doesn't use cumprod...

x <- c(1,2,3)
exp(cumsum(log(x)))

#> [1] 1 2 6

Try this with a loop:

#Code
v1 <- c(1,2,3)
#Empty vector
v2 <- numeric(length(v1))
#Loop
for(i in 1:length(v1))
{
  #Move around each element
  e1 <- v1[1:i]
  #Compute prod
  vp <- prod(e1)
  #Save
  v2[i] <- vp
}

Output:

v2
[1] 1 2 6

Tags:

R

Related

Install vue 3.0 in laravel Return value from list according to index number Precise specification of __await__ React/Typescript forwardRef types for an element which returns either an input or textArea iOS 14 SwiftUI Keyboard lifts view automatically Jetpack Compose State: Modify class property Building object models around external data Returning a list of every possible coordinate in a grid of that width and height Laravel 8 fresh installation livewire directory does not exist 'requestReview()' was deprecated in iOS 14.0 Xcode 12. Value of type 'AVCapturePhotoOutput' has no member 'supportedFlashModes' Partition N where the count of parts and each part are a power of 2, and part size and count are restricted

Recent Posts

Pandas how to find column contains a certain value Recommended way to install multiple Python versions on Ubuntu 20.04 Build super fast web scraper with Python x100 than BeautifulSoup How to convert a SQL query result to a Pandas DataFrame in Python How to write a Pandas DataFrame to a .csv file in Python
© 2021 newbedevPrivacy Policy