theamateurdataanalyst
theamateurdataanalyst

Reputation: 2834

Sum of factor frequency by week in R

Suppose I have the following data:

set.seed(123)
timeseq <- as.Date(Sys.time() + cumsum(runif(1000)*86400))
data <- sample(c("A","B","C"), 1000,replace = TRUE)
data.frame(timeseq,data)

Does anyone know how to find the counts of A,B,C by week?

Upvotes: 0

Views: 206

Answers (2)

Sangram
Sangram

Reputation: 417

If you want answer according to the week number then try this

set.seed(123)
timeseq <- as.Date(Sys.time() + cumsum(runif(1000)*86400))
data <- sample(c("A","B","C"), 1000,replace = TRUE)
data.frame(timeseq,data)

mydf <- as.Date(timeseq, format="%d-%m-%Y")
weeknum <- as.numeric( format(mydf+3, "%U"))
weeknum

new_data <- data.frame(timeseq,weeknum,data)

table(new_data$weeknum,new_data$data)

Upvotes: 0

chappers
chappers

Reputation: 2415

You can use lubridate to solve this. So using it with dplyr:

library(lubridate)

data.frame(timeseq,data) %>%
  mutate(week=floor_date(timeseq, "week")) %>%
  group_by(data, week) %>%
  tally

Which has output like this:

Source: local data frame [216 x 3]
Groups: data

   data       week n
1     A 2015-05-24 4
2     A 2015-05-31 3
3     A 2015-06-07 3
4     A 2015-06-14 5
5     A 2015-06-21 6
6     A 2015-06-28 3
7     A 2015-07-05 8
8     A 2015-07-12 3
9     A 2015-07-19 4
10    A 2015-07-26 6

Upvotes: 1

Related Questions