Merge csv files into one

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 correctly:

> 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 am going to Merge them by Date, the result appears as if it has two rows 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 do not know what's going on.

I actually wanted to merge all of the csv files I have into my directory and I can not think of a code for that.

    
asked by anonymous 30.01.2015 / 10:52

1 answer

5

I do not 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:

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

I do not know if that's exactly what you want.

    
30.01.2015 / 15:13