Join data frames

3

I have two date frames (see below) with consumption and gain information for animals. The consumption was collected every day and the gain in larger intervals. I want to join one next to the other, having a column of Animal, Day, Consumption and Gain. In that case, in gain, where it has no information, keep as NA. How can I do this?

DfConsumo
Animal	Dia	Consumo
5	2	1959.44
5	4	2015.125
5	5	2062.513
5	6	2102.862
5	7	2157.486
5	8	2213.519
5	9	2279.803
5	10	2317.222
5	11	2428.641
6	3	2000
6	4	2041.661
6	5	2049.473
6	6	2111.409
6	7	2149.92
6	8	2153.251
6	9	2207.271
6	10	2246.633
6	11	2297.115

DfGanho
Animal	Dia	Ganho
5	2	0.95
5	9	0.95
6	3	0.95
6	9	0.96
    
asked by anonymous 08.11.2017 / 15:31

1 answer

3

The merge function with the all = T option solves your problem. See code below:

DfConsumo <- data.frame(
  Animal = c(rep(5,9), rep(6,9)),
  Dia = c(2,4,5,6,7,8,9,10,11,3,4,5,6,7,8,9,10,11),
  Consumo = c(1959.44,2015.125,2062.513,2102.862,2157.486,2213.519,2279.803,2317.222,2428.641,
          2000,2041.661,2049.473,2111.409,2149.92,2153.251,2207.271,2246.633,2297.115)
)
DfGanho = data.frame(Animal = c(5,5,6,6), Dia = c(2,9,3,9), Ganho = c(.95,.95,.95,.96))

Df <- merge(DfConsumo, DfGanho, all = T)
    
08.11.2017 / 15:36