1

I have a list of vectors in R, and I want to use their variables as strings to name output files within a loop, but I can't manage to achieve that. I have used deparse(substitute(variables[index])) way, but it returns the string "variables[index]" Actually, I have this awful solution, and I want to remove variables_used vector...

basics<-c(8,12,16:18,23,56)
basicsndvi<-c(8,12,16:18,23,56,65,66)
volumes<-c(8,12,16,17,18,23,57:62,56)
volumesndvi<-c(8,12,16,17,18,23,57:62,56,65,66)
numbersNSEW<-c(8,12,16:18,23,42:45,56)
numbersNSEWndvi<-c(8,12,16:18,23,42:45,56,65,66)
all<-c(4,5,6,8,12,16:18,23,57:62,42:46,56)
allndvi<-c(4,5,6,8,12,16:18,23,57:62,42:46,56,65,66)

variables <- list(basics, basicsndvi, volumes, volumesndvi, numbersNSEW, 
numbersNSEWndvi, all, allndvi)

variables_used <- c("basics", "basicsndvi", "volumes", "volumesndvi", 
"numbersNSEW", "numbersNSEWndvi", "all", "allndvi")

// Here, I want to obtain the variable name as a String...
for(variable in variables){
    index = 1  
    ...
    ...
    ...
    outputFile.text <- paste0(parentPath, "/", selectedMethod, "/", 
    variables_used[index],".txt")
    index <- index + 1
}

Thanks in advance.

Julian G. M.
  • 25
  • 1
  • 5
  • `variables <- setNames(variables,variables_used)`? Then the names are part of the variables vector. You can create them up front in `list()` via `list(basics = basics, etc.)`. – joran Nov 26 '18 at 20:58

1 Answers1

1

One approach would be to use lst function from dplyr which will automatically set the names

library(dplyr)
variables <- lst(basics, basicsndvi, volumes, volumesndvi, numbersNSEW, 
                   numbersNSEWndvi, all, allndvi)

as @joran mentioned, the names get the names of the list

akrun
  • 874,273
  • 37
  • 540
  • 662