r - Efficient use of of vectors -


i attempting copying 1 vector using following syntax:

data<-null for( in 1:nrow(line)){   data=append(data,line[i*4]) } 

from have seen, use of append in way results in lot of copying of data, makes r slow. syntax copying 4th element of 1 array another, given list copying of given size?

here 3 methods benchmarks. can see preallocating vector in method2 function quite bit faster, while lapply method middle, , function slowest.

of course, these 1d vectors opposed arrays of n-d, expected benchmarks similar or more pronounced.

method1 <- function(line) {   data<-null   for( in 1:length(line)){     data=append(data,line[i])   } }  method2 <- function(line) {   data <- vector(mode="numeric", length = length(line))   (i in 1:length(line)) {     data[i] <- line[i]   } }  library(microbenchmark) r <- rnorm(1000) microbenchmark(method2(r), unit="ms") #> unit: milliseconds #>        expr     min       lq     mean   median       uq     max neval #>  method2(r) 2.18085 2.279676 2.428731 2.371593 2.500495 5.24888   100 microbenchmark(lapply(r, function(x) { data<-append(data, x) }), unit="ms") #> unit: milliseconds #>                                                    expr      min       lq #>  lapply(r, function(x) {     data <- append(data, x) }) 3.014673 3.091299 #>      mean   median       uq      max neval #>  3.287216 3.150052 3.260199 6.036501   100 microbenchmark(method1(r), unit="ms") #> unit: milliseconds #>        expr      min       lq    mean   median       uq      max neval #>  method1(r) 3.938684 3.978002 5.71831 4.020001 4.280521 98.58584   100 

didn't realize op wanted every fourth. why not use data frame or data.table?

d <- data.frame(matrix(rnorm(1000), ncol=1)) microbenchmark(d2 <- d[seq(1,nrow(d), 4),]) #> unit: microseconds #>                           expr    min      lq     mean median      uq #>  d2 <- d[seq(1, nrow(d), 4), ] 64.846 65.9915 73.08007 67.225 73.8225 #>      max neval #>  220.438   100 library(data.table) dt <- data.table(d) microbenchmark(d2 <- dt[seq(1,nrow(d), 4),]) #> unit: microseconds #>                            expr     min       lq     mean  median      uq #>  d2 <- dt[seq(1, nrow(d), 4), ] 298.163 315.2025 324.8793 320.554 330.416 #>      max neval #>  655.124   100 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -