how to make a table with event count and people count at the same time

I have a df that I would like to get the event count and people count by the result and relation.

The final outcome will have following info:

enter image description here

Currently I calculated each value and then fill them into matrix. I feel I might work in the wrong way amke a simple process too complicated. Could anyone give me some guidance on how to make this simple and neat?

The sample data are:

df<-structure(list(Pass = c("N", "N", "N", "N", "N", "N", "N", "Y", 
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", 
"N", "N", "Y", "N", "Y", "N", "Y", "N", "Y", "N", "N", "N", "N", 
"N", "N", "N", "N", "N", "N", "N", "N", "N", NA, "N", "N", "N", 
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", NA, "N", 
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N"), 
    Relation = c("UNRELATED", "UNRELATED", "UNRELATED", "UNRELATED", 
    "UNRELATED", "UNRELATED", "UNRELATED", "UNRELATED", "UNRELATED", 
    "UNRELATED", "UNRELATED", "RELATED", "RELATED", "RELATED", 
    "RELATED", "UNRELATED", "UNRELATED", "UNRELATED", "UNRELATED", 
    "UNRELATED", "RELATED", "UNRELATED", "UNRELATED", "UNRELATED", 
    "UNRELATED", "UNRELATED", "UNRELATED", "UNRELATED", "UNRELATED", 
    "UNRELATED", "UNRELATED", "UNRELATED", "UNRELATED", "UNRELATED", 
    "UNRELATED", "UNRELATED", "RELATED", "UNRELATED", "UNRELATED", 
    "UNRELATED", "UNRELATED", "UNRELATED", "UNRELATED", "UNRELATED", 
    "UNRELATED", "UNRELATED", "UNRELATED", "UNRELATED", "UNRELATED", 
    "RELATED", "RELATED", "UNRELATED", "UNRELATED", "RELATED", 
    "RELATED", "UNRELATED", "RELATED", "RELATED", "UNRELATED", 
    "RELATED", "UNRELATED", "UNRELATED", "UNRELATED", "RELATED", "RELATED", 
    "RELATED", "RELATED", "RELATED", "RELATED", "RELATED", "RELATED", 
    "RELATED"), ID = c("01-01-101", "01-01-101", "01-01-101", 
    "01-01-101", "01-01-101", "01-01-101", "01-01-101", "01-01-101", 
    "01-02-102", "01-02-102", "01-02-102", "01-02-102", "01-02-102", 
    "01-02-102", "01-02-102", "01-05-202", "01-05-202", "01-06-203", 
    "01-06-203", "01-11-402", "01-11-402", "01-11-402", "01-13-501", 
    "01-13-501", "01-13-501", "01-13-501", "01-13-501", "01-13-501", 
    "02-07-301", "02-07-301", "02-07-301", "02-07-301", "02-07-301", 
    "02-07-301", "02-07-301", "02-07-301", "02-07-301", "02-07-301", 
    "02-07-301", "02-07-301", "02-07-301", "02-07-301", "02-07-301", 
    "02-07-301", "02-07-301", "02-10-401", "02-10-401", "02-10-401", 
    "03-04-201", "03-04-201", "03-04-201", "03-04-201", "03-04-201", 
    "03-04-201", "03-04-201", "03-04-201", "03-09-303", "03-09-303", 
    "03-09-303", "03-09-303", "03-09-303", "03-09-303", "05-08-302", 
    "05-08-302", "05-08-302", "05-08-302", "05-12-403", "05-12-403", 
    "06-03-103", "06-03-103", "06-03-103", "06-03-103")), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -72L), spec = structure(list(
    cols = list(Pass = structure(list(), class = c("collector_character", 
    "collector")), Relation = structure(list(), class = c("collector_character", 
    "collector")), ID = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))

My current way to calculate each part are:

## Event count for related &  unrelated
events <- df %>% 
  mutate(Pass = factor(Pass, levels = c("N","Y", NA))) %>%
  group_by(Pass) %>% 
  summarise(count = paste0(n()))

# Event count for related
events_rl <- df %>% 
  filter (Relation=="RELATED" %>%
  mutate(Pass = factor(Pass, levels = c("N","Y", NA))) %>%
  group_by(Pass) %>% 
  summarise(count = paste0(n()))

# People count for related & unrelated
people <- df  %>% 
  distinct(ID,Pass) %>% group_by(Pass) %>% summarise(pcount = n())



from Recent Questions - Stack Overflow https://ift.tt/3u7TLhn
https://ift.tt/2XNx6dT

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)