How to join two csv files in R?

2

My problem is this: I have two csv data files with the same number of columns and the same column names (see below). I ask you to read the files as follows:

> dados_1 <- read.csv("Teste_01.csv")
> dados_2 <- read.csv("Teste_02.csv")

reading is done correctly (without errors), showing the result

> dados_1

A Ins Run Time.s Acc
S In1 1   141.60 0.902
S In1 2   150.20 0.905
S In2 1   180.10 0.887
S In2 2   161.50 0.898
P In1 1    75.37 0.840
P In1 2    92.28 0.879
P In2 1   170.90 0.833
P In2 2   114.00 0.833

> dados_2

A Ins Run Time.s Acc
S In1 1   171.50 0.899
S In1 2   166.90 0.887
S In2 1   154.30 0.889
S In2 2   167.10 0.844
S In3 1   162.50 0.915
S In3 2   156.00 0.880
P In1 1   121.50 0.859
P In1 2   122.20 0.874
P In2 1   159.10 0.856
P In2 2    73.70 0.872
P In3 1    10.95 0.857
P In3 2   119.00 0.848

I now need to merge the data from these two tables as follows: all data in the first table must be kept, and when joining the two tables the data in the first table must replace the data in the second table that have the same name in the columns "A" and "Ins", ie I need to get the following result

A Ins Run Time.s Acc
S In1 1   141.60 0.902     (veio de dados_1)
S In1 2   150.20 0.905     (veio de dados_1)
S In2 1   180.10 0.887     (veio de dados_1)
S In2 2   161.50 0.898     (veio de dados_1)
S In3 1   162.50 0.915     (veio de dados_2)
S In3 2   156.00 0.880     (veio de dados_2)
P In1 1    75.37 0.840     (veio de dados_1)
P In1 2    92.28 0.879     (veio de dados_1)
P In2 1   170.90 0.833     (veio de dados_1)
P In2 2   114.00 0.833     (veio de dados_1)
P In3 1    10.95 0.857     (veio de dados_2)
P In3 2   119.00 0.848     (veio de dados_2)

Does anyone know how I can do this? Thanks in advance.

    
asked by anonymous 09.10.2016 / 20:07

1 answer

1

I think the following code will solve your problem:

aux         <- dados_2[!(dados_2$Ins %in% unique(dados_1$Ins)) | 
!(dados_2$A %in% unique(dados_1$A)), ]
dados_final <- rbind(dados_1, aux)

The data frame dados_final created thus is not in the same order as your question, the results are identical.

    
09.10.2016 / 21:08