Sorry for what might be a basic / redundant question (with an awful title to boot). I have been struggling with calculating means of columns within data frames in a list. I've tried a variety of approaches mentioned in similar questions but can never get it to work. I'm relatively new to r and am a bit out of my depth.
I have a list of data frames similar to:
df1 <- data.frame(c("Jan", "Jan", "Jan"), c("21:14:33", "21:14:33", "21:14:33"), c(1, 2, 3), c(11, 12, 13))
df2 <- data.frame(c("Feb", "Feb", "Feb"), c("22:14:33", "22:14:33", "22:14:33"), c(2, 3, 4), c(12, 13, 14))
df3 <- data.frame(c("Mar", "Mar", "Mar"), c("23:14:33", "23:14:33", "23:14:33"), c(3, 4, 5), c(13, 14, 15))
mylist <- list(df1, df2, df3)
My goal is to create a vector for each data frame that contains the month, time, mean.column3, mean.column4. For example "Jan, 21:14:33, 2, 12" for the first data frame. (Ultimately I want to combine all these vectors into a new data frame, but I can do this once I have the vectors using rbind).
I have gotten the closest using for loops to calculate the mean, but using the below code it only gives me the mean for the last data frame (df3):
for(i in seq_along(mylist)){
output <- sapply(mylist[[i]][3:4], MARGIN = 2, FUN = mean)
}
I have also tried using lapply (as suggested here), abind (as suggested here), and map (as suggested here), which makes me think I'm the problem and must be missing something.
None of these approaches begins to address the need to include month and time in the resulting vector. I've tried to do it for a single data frame using code such as this, but it gives me all the months and times, when I really just need them once.
output1 <- c(mylist[[1]][1,1:2],sapply(mylist[[1]][3:4], MARGIN = 2, FUN = mean))
Help?