Exploratory Community

How to import multiple CSV files together as a single data frame

#1

Let’s say you have multiple CSV files at a folder or direcry and you want to import them together and create a single data frame. You can do something like this in ‘Import by Write R Script’ data source type.

library(tibble)
files = list.files(path = "~/Dropbox/Data/r_test", pattern = "*.csv", full.names = T)
files_df <- as_data_frame(files) %>% rownames_to_column() %>% rename(id = rowname)
tbl <- lapply(files, read_csv) %>% 
  bind_rows(.id = "id") %>% 
  left_join(files_df)

If anyone has a better way, I would love to hear as well.

#2

This works fine, but you need to import the tibble package otherwise the rownames_to_column() call fails.

#3

This would actually be a really handy built in feature. +1 for me. :slight_smile:

1 Like
#4

You’re right, Tony! We used to load ‘tibble’ package when opening projects, but stopped since we’re not exposing any of the functions through the column header menu or mutate function list.

Anyway, thanks for pointing that out!

#5

I’ve written a note about this.

English:

Japanese(日本語):

#6

thanks for the awesome information.

#7

Note:

You don’t need to write R script anymore!

You can simply do this with UI with the latest Exploratory!

-Kan