0

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":

enter image description here

#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"?

stats_noob
  • 5,401
  • 4
  • 27
  • 83

2 Answers2

1

There's no need for seq_along in this context - it's a helper function which prevents zero length vectors from crashing for loops. Since

  1. You're not in a for loop
  2. paste takes vectorised input

You can just do

names(list_data) <- paste('dfa', filenames, sep = '_')

If you want to add new numbers, you might do something like:

fileNumSuffix <- ( 1:length(filenames) ) + 100

names(list_data) <- paste('dfa', filenames, fileNumSuffix, sep = '_')

Captain Hat
  • 2,444
  • 1
  • 14
  • 31
1

If you want to create one combined dataframe from all the files you don't need to create separate dataframe objects.

all_dirs <- dir('Temp/', 'files_i_want', full.names = TRUE)

new_file <- do.call(rbind, lapply(unlist(lapply(all_dirs, 
                    list.files, full.names = TRUE)), read.table))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213