How to join two csv files in R?

Asked

Viewed 1,801 times

2

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

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

reading is done correctly (no 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

Now I need to join the data of these two tables as follows: all data of the first table should be maintained, and when joining the two tables the data of the first table should replace the data of the second table that are with the same name in the columns "A" and "Ins", that is, 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)

Can anyone tell me how to do this? Thanks in advance.

2 answers

2


I believe the code below solves 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 is not in the same order as your question, the results are identical.

0

You could solve this problem by using the function which to find the positions of the Ids (A and Ins) of the data_2 table that do not repeat in data_1.

posicao <- 
  which(!(dados_2$Ins %in% dados_1$Ins) |
          !(dados_2$A %in% dados_1$A))

# concatena linhas
dados_3 <- rbind(dados_1, dados_2[posicao, ])

dados_3

Browser other questions tagged

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