Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr: Combine multiple `count` + `mutate` statements per each variable into a single statement

Tags:

r

dplyr

Having data:

DT = structure(list(PE_RATIO = c(NA, 18.3468544431322, 21.8536295107188, NA, NA, NA), DIVIDEND_YIELD =c(NA, NA, 0.5283019, 1.06737822831035, NA, 0.55751900359546), DollarExposure = c(6765.12578958248, 95958.3106724681, 96328.1628155842, 291638.734002894, 170983.200676477, 185115.042371833)), .Names =c("PE_RATIO", "DIVIDEND_YIELD", "DollarExposure"), row.names = c(NA, -6L), class = c("data.table","data.frame"))
DT
#    PE_RATIO DIVIDEND_YIELD DollarExposure
# 1:       NA             NA       6765.126
# 2: 18.34685             NA      95958.311
# 3: 21.85363      0.5283019      96328.163
# 4:       NA      1.0673782     291638.734
# 5:       NA             NA     170983.201
# 6:       NA      0.5575190     185115.042

I would like to calculate weighted proportion of available values (called 'Capture') for multiple variables (here PE_RATIO and DIVIDEND_YIELD). I can do that in separate statements, one statement per variable:

DT %>% count(is.na(PE_RATIO), wt=abs(DollarExposure)) %>%
  mutate(PE_RATIO.Capture = prop.table(n))

# Source: local data table [2 x 3]
# 
# is.na(PE_RATIO)          n   PE_RATIO.Capture
# 1           FALSE 192286.5          0.2270773
# 2            TRUE 654502.1          0.7729227


DT %>% count(is.na(DIVIDEND_YIELD), wt=abs(DollarExposure)) %>%
  mutate(DIVIDEND_YIELD.Capture = prop.table(n))

# Source: local data table [2 x 3]
# 
# is.na(DIVIDEND_YIELD)          n   DIVIDEND_YIELD.Capture
# 1                 FALSE 573081.9                 0.676771
# 2                  TRUE 273706.6                 0.323229

Question:

How to combine multiple statements and achieve summary across the variables in a single dplyr statement? The desired output looks like this:

#         is.na(variable)  DIVIDEND_YIELD.Capture   PE_RATIO.Capture
# 1                 FALSE                0.676771          0.2270773
# 2                  TRUE                0.323229          0.7729227

Possibly, there will be half a dozen variables for which to calculate the capture ratio.

like image 592
Daniel Krizian Avatar asked Mar 19 '23 02:03

Daniel Krizian


1 Answers

try something like this

library(tidyr)
library(dplyr)
DT %>% gather(variable, value, -DollarExposure) %>% 
    group_by(variable, isna = is.na(value)) %>% 
    summarise(total = sum(abs(DollarExposure))) %>%
    group_by(variable) %>%
    mutate(prop = prop.table(total)) %>%
    ungroup %>%
    select(-total) %>%
    spread(variable, prop) 
# Source: local data frame [2 x 3]
# 
#    isna  PE_RATIO DIVIDEND_YIELD
# 1 FALSE 0.2270773       0.676771
# 2  TRUE 0.7729227       0.323229
like image 57
konvas Avatar answered Mar 26 '23 01:03

konvas