Home > Net >  Assign dynamic variable names to a list where each variable contains results
Assign dynamic variable names to a list where each variable contains results

Time:01-06

Context: I'd like to save the results of a Likelihood ratio test for a multinomial logistic regression in several dynamic variables, but I'm not sure how I could do that. This is what I've been trying:

library(lmtest)
indels = c("C.T","A.G","G.A","G.C","T.C","C.A","G.T","A.C","C.G","A.del","TAT.del","TCTGGTTTT.del","TACATG.del","GATTTC.del")


my_list = list()

for (i in 1:length(indels)) {
      assign(paste0("lrtest_results_",indels[i]), my_list[[i]]) = lrtest(multinom_model_completo, indels[i])
}

I was basically trying to save each variable (with the name lrtest_results_ the dynamic part of the variable name which depends on the vector indels) in a list using the assign method and paste0, but it doesn't seem to be working. Any help is very welcome!

CodePudding user response:

The best way is to lapply the test function to each element of the vector indels and assign the names after.

my_list <- lapply(indels, \(x) lrtest(multinom_model_completo, x))
names(my_list) <- paste0("lrtest_results_", indels)
  •  Tags:  
  • Related