Calculate average and deviation time series data

Asked

Viewed 310 times

3

I have a database of information of consumption of animals. The date of beginning of collection of consumption of each animal is different. Here is an example with only two animals:

Animal	Dia	Consumo
5	2	1379
5	3	2264
5	4	2234
5	5	2204
5	6	2369
6	3	1379
6	4	2264
6	5	2234
6	6	2204
6	7	2369

I need to calculate two things: 1) Calculate the average and standard deviations of consumption of all animals based only on the first information collected from animal.

2) Calculate the average and intake deviation of each animal based on the first three information collected from each animal. In this case I need to generate a new table (dataframe) similar to the one below:

Animal	Média	Desvio
5	x1	y1
6	x2	y2

If anyone can help me

1 answer

1


See if that’s what you need:

library(dplyr)
DfConsumo %>% 
  group_by(Animal) %>% 
  summarise(Consumo = first(Consumo)) %>% 
  summarise(Média = mean(Consumo), Desvio = sd(Consumo))

DfConsumo %>% 
  group_by(Animal) %>% 
  filter(row_number() <= 3) %>%
  summarise(Média = mean(Consumo), Desvio = sd(Consumo))

In the first part of the code, from what I understand of your problem, I take the first piece of information from each animal, and I calculate the average and the deviation of that information. In the second part, I select the first three pieces of information for each animal and calculate the averages and deviations per animal

Browser other questions tagged

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