I would like to get the mean of a variable according to the group it belongs to. Here is a reproducible example.
gender <- c("M","F","M","F")
vec1 <- c(1:4)
vec2 <- c(10:13)
df <- data.frame(vec1,vec2,gender)
variables <- names(df)
variables <- variables[-3]
#Wished result
mean1 <- c(mean(c(1,3)),mean(c(2,4)))
mean2 <- c(mean(c(10,12)),mean(c(11,13)))
gender <- c("M","F")
result <- data.frame(gender,mean1,mean2)
How can I achieved such a result ? I would like to use the vector variables, containing the names of the variables to be summarized instead of writing each variables, as my dataset is quite big.