I am using the R programming language. In a previous question (R: Importing Entire Folder of Files) , I learned how to import all the files from a given folder and name them in "order":
#Get the path of filenames
filenames <- list.files("C:/Users/Documents/files_i_want", full.names = TRUE)
#Read them in a list
list_data <- lapply(filenames, read.table)
#Name them as per your choice (df_1, df_2 etc)
names(list_data) <- paste('df', seq_along(filenames), sep = '_')
#Create objects in global environment.
list2env(list_data, .GlobalEnv)
This imports all the files from this folder and names them as "df_1", "df_2", "df_3", etc. It would appear that the "seq_along" statement adds the "1", "2", "3" for each of these files.
Is there a alter the "seq_along" statement such that it starts at some different number? Could the first file be named as "df_100" followed by "df_101", "df_102" ... or "df_a1, df_a2", df_a3"?
The reason I am asking this : Suppose I now have files in two different folders ("files_i_want_a", "files_i_want_b") that I want to import into R. I could modify the answer as follows:
#first folder
filenames <- list.files("C:/Users/Documents/files_i_want_a", full.names = TRUE)
#Read them in a list
list_data <- lapply(filenames, read.table)
names(list_data) <- paste('dfa', seq_along(filenames), sep = '_')
#Create objects in global environment.
list2env(list_data, .GlobalEnv)
#second folder
filenames <- list.files("C:/Users/Documents/files_i_want_b", full.names = TRUE)
#Read them in a list
list_data <- lapply(filenames, read.table)
names(list_data) <- paste('dfb', seq_along(filenames), sep = '_')
#Create objects in global environment.
list2env(list_data, .GlobalEnv)
Would there be a more efficient way to combine all these imported files into a single file?
e.g. new_file = rbind(dfa_1, dfa_2, dfa_3, dfb_1, dfb_2, dfb_3)
If these files were not named differently by writing paste('dfb', seq_along(filenames), sep = '_')
vs. paste('dfa', seq_along(filenames), sep = '_')
, they would all have been overwritten and deleted each time new files were brought in.
Is there a direct command that can automatically "rbind" all files that have been imported into the local environment, and assign them to some new object such as "new_file"?