1

I have a data set. as an example, a data for a year of 2016.

Let's say that there is 365 observations, from Jan 1st to Dec 31st of 2016. each day, the data includes either one or zero.

I am trying to calculate the percentage of ones for each month.

I will appreciate for helps experts!

  • It would be easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and the desired output for that input so that possible solutions can be tested. – MrFlick Jul 19 '17 at 15:58
  • Suggested dupe: [average data by group](https://stackoverflow.com/q/11562656/903061) – Gregor Thomas Jul 19 '17 at 16:01
  • Also see https://stackoverflow.com/questions/33105900/sum-daily-values-into-monthly-values – lmo Jul 19 '17 at 16:08

1 Answers1

1

This should work:

df = data.frame(date=seq(as.Date("2017-01-01"),as.Date("2017-12-31"),by=1) , value=sample(c(0,1),365,replace=T) )

library(dplyr)
df  = df %>% mutate(month = format(date,"%m")) %>%  # or %b for month abbreviation
group_by(month) %>% 
summarize(value=sum(value)/length(value)) 
Florian
  • 24,425
  • 4
  • 49
  • 80
  • In base R: `do.call(rbind, lapply(split(df, format(df$date, "%m")), function(a) data.frame(month = format(a$date[1], "%b"), percentage = sum(a$value)/NROW(a))))` – d.b Jul 19 '17 at 16:09
  • Thanks for sharing, nice solution. I wasn't aware of the possibility to do this in base R. – Florian Jul 19 '17 at 16:14
  • 1
    @d.b If you're gonna use `base`, use `aggregate(value ~ format(date,"%m"), mean, data = df)` – Gregor Thomas Jul 20 '17 at 00:26