[R] Alternative to apply in base R
jfox at mcmaster.ca
Tue Nov 8 21:00:10 CET 2016
If the actual data with which you're dealing are non-negative, you could log all the values, and use colSums() on the logs. That might also have the advantage of greater numerical accuracy than multiplying millions of numbers. Depending on the numbers, the products may be too large or small to be represented. Of course, logs won't work with your toy example, where rnorm() will generate values that are both negative and positive.
I hope this helps,
John Fox, Professor
Canada L8S 4M4
From: R-help [r-help-bounces at r-project.org] on behalf of Doran, Harold [HDoran at air.org]
Sent: November 8, 2016 10:57 AM
To: r-help at r-project.org
Subject: [R] Alternative to apply in base R
Without reaching out to another package in R, I wonder what the best way is to speed enhance the following toy example? Over the years I have become very comfortable with the family of apply functions and generally not good at finding an improvement for speed.
This toy example is small, but my real data has many millions of rows and the same operations is repeated many times and so finding a less expensive alternative would be helpful.
mm <- matrix(rnorm(100), ncol = 10)
rn <- apply(mm, 1, prod)
[[alternative HTML version deleted]]
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help