1

I am trying to merge some csv files into one large data frame using the following code:

library(tidyverse)

list_of_files = list.files(path = "/home/maanan/Downloads/NYCData/2017/", recursive = TRUE, pattern = "\\.csv$", full.names = TRUE)

df = readr::read_csv(list_of_files)

The code above works fine when the column names are the same. But when I try to merge data sets which have different column names it gives me the following error:

Error: Files must have consistent column names:                                                                                                                               
* File 1 column 1 is: Trip Duration
* File 4 column 1 is: tripduration

I am trying to combine 12 monthly data sets, 6 of them have names like this:

"Trip Duration"  "Start Time" "Stop Time" "Start Station ID" "Start Station Name"     

The other 6 data sets have names like this:

"tripduration"  "starttime"  "stoptime"  "start station id"  "start station name"

I would be grateful if someone could help me solve this problem. Thank you.

Saïd Maanan
  • 511
  • 4
  • 14

1 Answers1

0

You can specify different name columns by using by.x and by.y where x and y are the data frames.

Just ?merge to see how to use them.

flopeko
  • 149
  • 8
  • `read_csv` allows me to read and compile multiple files into one data frame without importing them into `r` environment, and this is crucial for me because my computer's memory is limited and the files are very large. `merge` cannot read and merge at the same time. – Saïd Maanan Mar 24 '22 at 07:15