Reputation: 5049
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)
}
}
Upvotes: 0
Views: 302
Reputation: 5254
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])
.
Upvotes: 1