Hi there! I would like to know how can I transform a dataframe to have the average of some variables depending on groups determined by other variables. In this case there are 4 groups, one per every state and for each year. I have this data:
state<- c( Alabama, Alabama, Alabama, Alabama, Arkansas, Arkansas, Arkansas, Arkansas)
year<- c(2000, 2000, 2001, 2001, 2000, 2000, 2001, 2001)
gender <- c(1, 0, 1, 0, 1, 0, 1, 0)
age65 <- c(0, 1, 0, 1, 0, 1, 0, 1)
df1 <- data.frame(state, year, gender, age65)
I would like to obtain this data, getting the average of the values of gender and age65
state<- c( Alabama, Alabama, Arkansas, Arkansas)
year<- c(2000, 2001, 2000, 2001)
gender <- c(0.5, 0.5, 0.5, 0.5)
age65 <- c(0.5, 0.5, 0.5, 0.5)
df2 <- data.frame(state, year, gender, age65)
Please note that in my original dataset i have multiple states and years, so I would like to obtain a code that does not contain specific observation names but variable names.
Thanks!