I know I have a simple problem here, but I can't seem to get it. I have multiple .txt files that I would like to use read.table on by looping, in order for each .txt file to be its own data.frame. The issue comes in regards to being able to read in each .txt file name with its own unique variable name
For example:
path = "dir" #my directory
filenames <- list.files(path)
for (i in 1:length(filenames))
{
ID <- filenames[i]
ID <- paste(ID, "average_table", sep = "_")
read.table(filenames[i], header=TRUE) # error about "cannot open connection" and "cannot open file ID: no such file or directory"
}
For example, if I have sub1.txt, sub2.txt, sub3.txt ... I would like to simply read each one in as a data.frame, each with having the variable name of its .txt file with the "average_table" added at the end (e.g. first loop through with sub1.txt would lead to a data.frame variable of sub1_average_table).
I've used lapply, but this of course creates a list, which is not what I want. What would be the best way then to read in each file, while naming its corresponding data.frame to the .txt file name? Thank you.