Organize a time series

4

I have a base with information collected on weight gain per animal. They were collected at different intervals for each animal. Follow part of the base just below

Animal	Dia	Ganho
5	2	0.99
5	9	1.01
5	16	1.03
5	23	1.08
5	30	1.08
5	37	1.07
6	5	0.99
6	10	1.01
6	17	1.02
6	24	1.04
6	33	1.07
6	38	1.08

First I need to put in one-day intervals for each animal. Here is an example below, you can stay NA on days that have no information.

Animal	Dia	Ganho
5	2	0.99
5	3	NA
5	4	NA
5	5	NA
5	6	NA
5	7	NA
5	8	NA
5	9	1.01

Finally, I need to remove only the first three days of each animal from the base.

    
asked by anonymous 27.11.2017 / 10:07

1 answer

3

I used the dplyr and tidyr packages to solve your problem.

dados <- data.frame(
  Animal = c(rep(5,6), rep(6,6)), 
  Dia = c(2,9,16,23,30,37,5,10,17,24,33,38),
  Ganho = c(0.99,1.01,1.03,1.08,1.08,1.07,0.99,1.01,1.02,1.04,1.07,1.08)
)

library(dplyr)
library(tidyr)

dados2 <- dados %>%
  group_by(Animal) %>% 
  complete(Dia = seq(from = min(Dia), to = max(Dia), by = 1), fill = list(Ganho = NA)) %>% 
  slice(4:n())
    
27.11.2017 / 12:24