# Calculate mean and time series data deviation

## Question:

I have an animal consumption information database. The start date of consumption collection for 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 mean and standard deviations of consumption for all animals based only on the first information collected from the animal.

2) Calculate the average and deviation of consumption 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

See if this is 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 1st part of the code, from what I understand about your problem, I get the 1st information of each animal, and I calculate the average and the deviation of this information. In the second part, I select the first three pieces of information for each animal and calculate the means and deviations per animal.

Scroll to Top
AllEscort