Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does this loop and append to data frame not work?

I’ve done a similar operation many times, yet this example has me perplexed. Why does this not work? My expected output is an avg per row of avgs

library(dplyr)

for(i in 1:3) {
  avgs = data_frame('avgs' = rep(NA,3))
  avg = mean(rnorm(100, .6 , 6.8))
  avgs[i, ] = avg
}
avgs

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

fixing your code:

library(dplyr)

avgs = data_frame('avgs' = rep(NA,3))
for(i in 1:3) {
  avg = mean(rnorm(100, .6 , 6.8))
  avgs$avgs[i] = avg
}
avgs

Why avoid loops:

library(microbenchmark)

rows = 3
avgs = data_frame('avgs' = rep(NA,rows))

loop <- function(df) {
  for(i in 1:rows) {
    avg = mean(rnorm(100, .6 , 6.8))
    df$avgs[i] = avg
  }
  return(df)
}

vectorize_it <- function(df) {
  df$avgs <- replicate(rows,mean(rnorm(100, .6 , 6.8)))
  return(df)
}

microbenchmark::microbenchmark(loop(avgs),vectorize_it(avgs))

rows = 3e3
avgs = data_frame('avgs' = rep(NA,rows))

microbenchmark::microbenchmark(loop(avgs),vectorize_it(avgs))
Unit: microseconds
               expr     min       lq     mean   median      uq      max neval
         loop(avgs) 242.936 252.3650 392.8930 271.5235 326.749 4987.736   100
 vectorize_it(avgs) 138.131 142.1365 290.6037 155.3900 184.079 4804.938   100
> 
> rows = 3e3
> avgs = data_frame('avgs' = rep(NA,rows))
> 
> microbenchmark::microbenchmark(loop(avgs),vectorize_it(avgs))
Unit: milliseconds
               expr       min        lq      mean    median        uq      max neval
         loop(avgs) 281.02566 294.74691 345.14865 315.18423 347.36404 847.0420   100
 vectorize_it(avgs)  41.90678  43.51001  55.31815  47.26524  56.92735 167.4951   100
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading