0

Having 2 or more file paths like below each to be read as separate data frames

file1 = ".data/abc_123.txt"
file2 = ".data/def_324.txt"

To enable batch reading, storing these filenames in to a vector filesVector = c(file1, file2)

Inside the function used to batch read files, need to access the variable names that are in filesVector

csvToDF = function(filesVector){
  for(file in filesVector){ 
    # is there a way to extract variable names `file1` & `file2` inside here so as to create a dataframe with name of file as part of the variable for variable
    # in the above example data, it should create two data frames stored as variables `df_file1` and `df_file2`
    variable_name = read.csv(file)
  }
}
1
  • try eval(substitute(file)) Commented May 23, 2020 at 15:17

1 Answer 1

1

Doing it the way you do the variable names get lost. But as workaround you could name vector elements before you call the function:

names(filesVector) <- c("file1", "file2")

Now you should be able to access these inside the function simply with names(filesVector) or names(filesVector[1]).

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.