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.

1
  • 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.). Commented Nov 26, 2018 at 20:58

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.