R language: how to work with dynamically sized vector? -


i'm learning r programming, , trying understand best approach work vector when don't know final size end being. example, in case need build vector inside for loop, iterations, aren't know beforehand.

method 1

i run through loop first time determine final vector length, initialize vector correct length, run through loop second time populate vector. ideal memory usage standpoint, since vector memory occupy required amount of memory.

method 2

or, use 1 for loop, , append vector needed, inefficient memory allocation standpoint since new block may need assigned each time new element appended vector. if you're working big data, problem.

method 3

in c or matlab, initialize vector length largest possible length know final vector occupy, populate subset of elements in for loop. when loop completes, i'll re-size vector length appropriately.

since r used lot in data science, thought topic others have encountered , there may best practice recommended. thoughts?

canonical r code use lapply or similar run function on each element, combine results in way. avoids need grow vector or know size ahead of time. functional programming approach things. example,

set.seed(5) x <- runif(10)  some_fun <- function(x) {   if (x > 0.5) {     return(x)   } else {     return(null)   } }  unlist(lapply(x, some_fun)) 

the size of result vector not specified, determined automatically combining results.

keep in mind trivial example illustration. particular operation vectorized.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -