Merge csv files into one

Asked

Viewed 2,623 times

4

I have several csv data files that have a common column named "Data". If I read two files from this directory the reading is done in the correct way:

> P1<-read.csv("02650019.csv", header = TRUE, sep = ";")
> P2<-read.csv("02650032.csv", header = TRUE, sep = ";")

> head(P1)

  Data       X2650019
1 1986-06-01        0
2 1986-06-02        0
3 1986-06-03        0
4 1986-06-04        0
5 1986-06-05        0
6 1986-06-06        0

> head(P2)

        Data X2650032
1 2000-04-01       NA
2 2000-04-02       NA
3 2000-04-03       NA
4 2000-04-04       NA
5 2000-04-05       NA
6 2000-04-06       NA

But when I go to merge them by Data, the result appears as if it had two lines of each data and does not appear in the temporal order:

> merge1 <- merge(P1, P2, by = c("Data"), all = T)
> head(merge1)

        Data X2650019 X2650032
1 1976-07-01       NA       NA
2 1976-07-01       NA       NA
3 1976-07-02       NA       NA
4 1976-07-02       NA       NA
5 1976-07-03       NA       NA
6 1976-07-03       NA       NA

I don’t know what’s going on.

Actually I wanted to merge all the csv files I have in my directory and can’t think of a code for it.

  • It wouldn’t be something to do with the name of the column?

  • Please transfer the command output dput(head(P1)) and dput(head(P2)) to better understand how the data are structured. Another thing: this date 197-07-01 appears in P1 or P2?

1 answer

5

I don’t have your files here to test, but I would do something like this:

setwd("C://...") # caminho par ao seu diretório
arquivos <- list.files(path = "C://..", pattern = "*.csv") # caminho para o diretório e extensao deles

bases <- lapply(arquivos, function(x){
  b <- read.csv(x, header = TRUE, sep = ";")
  names(b) <- c("data", "x")
  b
})
library(dplyr)
rbind_all(bases)

The idea is more or less this:

  • put the working directory as the file directory
  • I read the name of all files in the directory, with extension csv
  • I read all the files to R and put all the names in the same pattern
  • pile all

I don’t know if that’s exactly what you want.

  • 1

    off-topic: interesting to know that we have closures in the R

  • Excellent answer. By the question tb I understood that rbind() was what was being asked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.