Stop wasting your time manually importing multiple files
In my data scientist job, I usually should import several different files that contain the identical form of information as a result of export constraints in several software. For those who are in an identical situation, below is a transparent and straightforward technique to have the option to mechanically import your files as individual data frames or mix them right into a single data frame.
Before we start with our code, we first must prepare our files. We want to have a technique to programmatically select the files that we would like to import into R. While you would select any technique to distinguish these files, listed below are two of the best ways:
- Create a novel prefix on the entire files that you wish to import without delay.
- Create a separate folder in your working directory and only include those files in that folder.
For instance, if I had a set of Excel files called “SA#.xlsx”. If I had no other similar files that began with SA, then I have already got my prefix. If there are other files in my folder that start with SA reminiscent of “SAT.xlsx”, I can easily create a folder and I’ll name it “SA”. Then, I’ll only include the files I would like to import as SA into that folder.
Once now we have a programmatic technique to discover our files, we’d like to create a listing of the entire file names. We are able to use the R function list.files() to attain this.
File list with prefix
For those who select so as to add a prefix to your file names, we’ll use the pattern parameter of list.files() to pick the particular files that we would like.
# Formula
filelist <- list.files(pattern = "^")#Example
filelist <- list.files(pattern = "^SA")
The pattern takes in a daily expression. Due to this fact, we will use the “^” symbol to represent the start of the string. This ensures that every other file names that include “SA” inside the name but not at the start is not going to be included on this set of names. Note: It will only pull files out of your working directory. You may change the…