1

I have a some data with month and sales. Is there way I can loop over all the months and get the mean

I'm not sure how to do this still very new at R

 df2 <- df[grep("Jan", df$Month),]
 mean(df2$Sales)

Rather than typing each month every time. Is there a way I can just get R to loop over all the months and give a mean for each month

stacker
  • 85
  • 1
  • 8
  • it's hard, but u need something like that `library(dplyr) df %>% filter(grepl("Jan", Month)) %>% group_by (Month) %>% summarize(mean_sales = mean(Sales))` – jyjek Sep 18 '19 at 14:39
  • 1
    `with(df, tapply(Sales, Month, mean))` might work, but seeing he result of `dput(head(df))` would make answering the question easier – Ape Sep 18 '19 at 14:42
  • for(m in month.abb){df2 <- df[grep(m, df$Month),] print(paste(m, mean(df2$Sales), sep=" "))} – Shirin Yavari Sep 18 '19 at 23:35

0 Answers0