Growing a list with variable names in R -
Growing a list with variable names in R -
i trying grow list in r, both value , name of each entry held in variable, doesn't seem work.
my_models_names <- names(my_models) my_rocs=list() (modl in my_models_names) { my_probs <- testpred[[modl]]$y1 my_roc <- roc(ytst, my_probs) c(my_rocs, modl=my_roc) # <-- modl , my_roc both variables }
my list my_rocs
empty @ end, though know loop iterates (my_roc
filled in) why?
on related note, there way without looping?
generally in r, growing objects bad. increases amount of memory used on starting total object , filling in. seems know size of list should in advance.
for example:
my_keys <- letters[1:3] mylist <- vector(mode="list", length=length(my_keys)) names(mylist) <- my_keys mylist ## $a ## null ## $b ## null ## $c ## null
you can assignment way:
key <- "a" mylist[[key]] <- 5 mylist ## $a ## [1] 5 ## ## $b ## null ## ## $c ## null
r list
Comments
Post a Comment