I've searched a lot to answers about this but so far I'm really struggling. I have 5 data frames, all with 6 rows and 10 columns. I would like to create a new data frame with the same structure and with the means for each position on the 5 data frames. I've tried this:
age_wealth1 <- data.frame(age_wealth1)
age_wealth2 <- data.frame(age_wealth2)
age_wealth3 <- data.frame(age_wealth3)
age_wealth4 <- data.frame(age_wealth4)
age_wealth5 <- data.frame(age_wealth5)
age_wealth_total <- matrix(nrow = nrow(age_wealth1), ncol = ncol(age_wealth1))
for(j in 1:nrow(age_wealth_total[j]))
{
for(k in 1:ncol(age_wealth_total[k]))
{
age_wealth_total[j,k] <- mean(age_wealth1[j,k], age_wealth2[j,k], age_wealth3[j,k], age_wealth4[j,k], age_wealth5[j,k])
}
}
The loop gives error about the columns length, saying that is 0, which is not true because
ncol(age_wealth_total) # = 10
I really would like to have more tips about how to iterate over columns and rows in a matrix or a data frame using loops. Thanks a lot!