반응형

 

not working

- date_cnt_mismatch 가 항상 0 이 나온다.

- 이유: n_distinct 를 하는 과정에서 NA 가 포함되면 0을 반환함 

user_table <- anal_table %>% group_by(account_id, gender) %>% summarize(
  log_cnt = sum(1),
  log_cnt_mismatch = sum(if_else(ip_check == 1, 1, 0)),
  date_cnt = n_distinct(date_id),
  date_cnt_mismatch = n_distinct(if_else(ip_check == 1, date_id, NA))
)

 

 

working

- 간단하게 sql 을 활용해서 구현할 수 있다.

user_table <- anal_table %>% group_by(account_id, gender) %>% summarize(
  log_cnt = sum(1),
  log_cnt_mismatch = sum(if_else(ip_check == 1, 1, 0)),
  date_cnt = n_distinct(date_id),
  date_cnt_mismatch = sql('count(distinct(if(ip_check == 1, date_id, null)))')
)

 

반응형