R: Manually Specifying Factor Levels

Solution 1:

Is this helpful?

library(dplyr)

problem_data %>%
  group_by(types) %>%
  count(dates)
#> # A tibble: 25 × 3
#> # Groups:   types [5]
#>    types dates       n
#>    <fct> <fct>   <int>
#>  1 A     2010-01   188
#>  2 A     2010-02    77
#>  3 A     2010-03    35
#>  4 A     2010-04    32
#>  5 A     2010-05    31
#>  6 B     2010-01   137
#>  7 B     2010-02    64
#>  8 B     2010-03    27
#>  9 B     2010-04    28
#> 10 B     2010-05    20
#> # … with 15 more rows

Created on 2022-01-23 by the reprex package (v2.0.1)

data:

set.seed(111)
v1 <- c("2010-01", "2010-02", "2010-03", "2010-04", "2010-05")
v2 <- c("A", "B", "C", "D", "E")
dates <- as.factor(sample(v1, 1000, replace = TRUE, prob = c(0.5, 0.2, 0.1, 0.1, 0.1)))
types <- as.factor(sample(v2, 1000, replace = TRUE, prob = c(0.3, 0.2, 0.1, 0.1, 0.1)))
var <- rnorm(1000, 10, 10)
problem_data <- data.frame(var, dates, types)