Hi
there are some good tips in "The R Inferno"
http://www.burns-stat.com/documents/books/the-r-inferno/
or connect C++ to R with Rcpp
http://dirk.eddelbuettel.com/code/rcpp.html
or byte code compiler (library(compiler))
or library(data.table)
but do you have an idea to fasten standard R source code, with the
following Rprof output
self.time self.pct total.time total.pct
[<-.data.frame 1.34 29.13 1.78 38.70
[.data.frame 0.26 5.65 1.02 22.17
[[ 0.12 2.61 0.44 9.57
NextMethod 0.12 2.61 0.12 2.61
match 0.10 2.17 0.16 3.48
<Anonymous> 0.10 2.17 0.10 2.17
c 0.10 2.17 0.10 2.17
[[.data.frame 0.08 1.74 0.32 6.96
[.Date 0.06 1.30 0.10 2.17
FUN 0.06 1.30 0.10 2.17
[<- 0.04 0.87 1.82 39.57
[ 0.04 0.87 1.04 22.61
[<-.Date 0.04 0.87 0.18 3.91
vapply 0.04 0.87 0.14 3.04
%in% 0.02 0.43 0.18 3.91
+ 0.02 0.43 0.10 2.17
It comes from a simulation algorithmus that calculates day wise values
(values are depenend from the output of the day before). First I create
a data.frame with NAs. Finally each row contains the daily values.
output <- as.data.frame(matrix(nrow = 365, ncol = 50))
for (day in (1:365)) {
...
r <- list(Date=d,daylength=daylength,TempSum=tempsum, ...)
output[day,] <- r
}
Is there an better (faster) way to do such things in R?
Greetings
Christof
On 13-03-28 7:07 AM, Christof Klu? wrote:> Hi > > there are some good tips in "The R Inferno" > http://www.burns-stat.com/documents/books/the-r-inferno/ > or connect C++ to R with Rcpp > http://dirk.eddelbuettel.com/code/rcpp.html > or byte code compiler (library(compiler)) > or library(data.table) > > but do you have an idea to fasten standard R source code, with the > following Rprof output > > self.time self.pct total.time total.pct > [<-.data.frame 1.34 29.13 1.78 38.70 > [.data.frame 0.26 5.65 1.02 22.17 > [[ 0.12 2.61 0.44 9.57 > NextMethod 0.12 2.61 0.12 2.61 > match 0.10 2.17 0.16 3.48 > <Anonymous> 0.10 2.17 0.10 2.17 > c 0.10 2.17 0.10 2.17 > [[.data.frame 0.08 1.74 0.32 6.96 > [.Date 0.06 1.30 0.10 2.17 > FUN 0.06 1.30 0.10 2.17 > [<- 0.04 0.87 1.82 39.57 > [ 0.04 0.87 1.04 22.61 > [<-.Date 0.04 0.87 0.18 3.91 > vapply 0.04 0.87 0.14 3.04 > %in% 0.02 0.43 0.18 3.91 > + 0.02 0.43 0.10 2.17 > > > It comes from a simulation algorithmus that calculates day wise values > (values are depenend from the output of the day before). First I create > a data.frame with NAs. Finally each row contains the daily values. > > output <- as.data.frame(matrix(nrow = 365, ncol = 50)) > for (day in (1:365)) { > ... > r <- list(Date=d,daylength=daylength,TempSum=tempsum, ...) > output[day,] <- r > } > > Is there an better (faster) way to do such things in R?Generally matrices are much faster than dataframes. Your code spends about 35-60% of its time in dataframe indexing methods, so you might be able to double the speed by switching to matrices. The disadvantage is that matrices can only hold one type, so you may need multiple matrices to replace one dataframe, and that will make your code harder to read. Duncan Murdoch