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

Create lots of null containers in R, using a simple code

I’m creating empty null containers, to collect values from a "for loop".
Here is an example in my code, for which I’ve created 6 empty containers.

cont_min  <- NULL
cont_max  <- NULL
cont_unused <-  NULL
cont_n_rec  <- NULL
cont_n_don  <- NULL
cont_loop_time <- NULL

I need to create quite a few of these and it’s using a lot of space using a line of code for each.
Is there a way to create multiple containers without having a separate line of code? Many thanks

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

>Solution :

You could use something like this:

names <- c('min', 'max', 'unused', 'n_rec', 'n_don', 'loop_time')
var_lst <- sapply(paste0('cont_', names), function(x) assign(x, NULL))
list2env(var_lst, .GlobalEnv)
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