read all files in a directory into a data frame, with a column recording filename using tidyverse in r

To read all files in a directory into a dataframe, with a column recording filename in R with the Tidyverse package, we can use the following code:

main.r
library(tidyverse)

# Set directory path
dir_path <- "path/to/directory"

# Create a dataframe to store the data
data_frame <- data.frame()

# Loop through all files in the directory
for (file in list.files(path = dir_path, full.names = TRUE)) {
  
  # Read the file into a temporary dataframe
  temp_df <- read_csv(file, col_types = cols())
  
  # Extract the filename
  filename <- str_extract(file, "\\w+\\.csv$")
  
  # Add filename as a new column
  temp_df <- mutate(temp_df, filename = filename)
  
  # Append the temporary dataframe to the main dataframe
  data_frame <- bind_rows(data_frame, temp_df)
}

# Preview the data
head(data_frame)
649 chars
27 lines

This code will read all .csv files in the specified directory into a dataframe, with a new column indicating the filename for each row of data. Note that we are using read_csv() from the readr package to read the files, and bind_rows() from the dplyr package to combine the dataframes. We are also using str_extract() from the stringr package to extract the filename from the file path. Don't forget to replace "path/to/directory" with the actual directory path you want to use.

related categories

gistlibby LogSnag