I am trying to count records on summarize given conditions as the code bellow but it is not working. I am trying to count total rows, not a distinct count.
What am I doing wrong?
library(dplyr)
data %>%
group_by(UNIT) %>%
summarize(TOTAL = n(),
SLA = n(DATETIME[TYPE=='A' || TYPE=='B']))
Appreciate any help
CodePudding user response:
use sum
instead of n
library(dplyr)
set.seed(123)
data <- data.frame(
UNIT = sample(1:3, size = 100, replace = TRUE),
TYPE = sample(c('A', 'B', 'C'), size = 100, replace = TRUE)
)
head(data)
#> UNIT TYPE
#> 1 3 C
#> 2 3 B
#> 3 3 B
#> 4 2 C
#> 5 3 A
#> 6 2 A
data %>%
group_by(UNIT) %>%
summarise(TOTAL = n(), SLA = sum(TYPE == 'A' | TYPE == 'B'))
#> # A tibble: 3 × 3
#> UNIT TOTAL SLA
#> <int> <int> <int>
#> 1 1 33 22
#> 2 2 32 19
#> 3 3 35 27
Created on 2022-02-18 by the reprex package (v2.0.1)