I have a dataset with the following structure
year var cod localidad
34 1999-12-01 82.5 S1 9 de Julio
35 1999-12-11 64.4 S1 9 de Julio
36 1999-12-21 58.4 S1 9 de Julio
37 2000-01-01 43.0 S1 9 de Julio
38 2000-01-11 43.5 S1 9 de Julio
39 2000-01-21 20.8 S1 9 de Julio
40 2000-02-01 24.5 S1 9 de Julio
41 2000-02-11 50.0 S1 9 de Julio
42 2000-02-21 51.6 S1 9 de Julio
43 2000-03-01 52.5 S1 9 de Julio
44 2000-03-11 52.4 S1 9 de Julio
45 2000-03-21 74.7 S1 9 de Julio
I have two var values for the same year , one corresponding to S1 and the other to S2 of the cod variable . I need to calculate the mean of var between the two cod values for all observations. How can I do that?
With
dplyr/tidyverse
you can do:We group
data.frame
by locality and year, and we summarize in the new columnmedia
the mean value ofvar
. In base R, something similar could be:aggregate(var ~ ., df, mean)
.