I am trying to create a function with one of column of my dataframe outplacement, because similar calculations I would need for other columns also.
numeric_fun<-function(dataset,grp_var,var){
require("dplyr")
dataset%>%select(grp_var,var)%>% group_by_(grp_var)%>%
summarize(
'q25' = quantile(var, type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[2],
'median' =round(quantile(var, type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[3],0),
'avg' = round( mean(var, na.rm=TRUE), 0),
'q75' = quantile(var, type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[4] ,
'n' = sum(!is.na(var))
)%>%
mutate(
q25 = ifelse( n < 5, "--", paste(q25,"%" )),
median = ifelse( n < 5, "--", paste(median,"%")),
avg = ifelse(n < 5, "--", paste(avg,"%")),
q75 = ifelse( n < 5, "--", paste(q75,"%")),
n = n
) %>%
rename( "Industry"=grp_var,
"25%" = q25,
"75%" = q75
)
}
numeric_fun(outplacement,"Q7_1","Q8")
But this ones is not working, any other efficient ways?
output of dput
structure(list(Q7_1 = structure(c(NA, NA, NA, 5L, 5L, 14L, NA,
1L, 9L, 13L, 1L, NA, 3L, 13L, 13L, 13L, 12L, 2L, 11L, 13L, 10L,
11L, NA, 1L, 4L, NA, 5L, NA, 5L, 4L, 5L, 9L, 2L, 4L, 13L, 10L,
13L, 13L, NA, 11L, NA, 1L, 11L, NA, 5L, NA, 1L, NA, 9L, 3L, 1L,
1L, 10L, 1L, NA, 5L, NA, NA, 2L, NA, 6L, 6L, NA, 13L, 14L, NA,
NA, 14L, 8L, 11L, 11L, 8L, 12L, 13L, NA, 3L, 11L, 3L, 11L, 1L,
NA, 9L, NA, 10L, 6L, 1L, 5L, 3L, 1L, 13L, 4L, 14L, NA, 13L), .Label = c("Banking/Financial Services",
"Chemicals", "Consumer Goods", "Energy", "High Tech", "Insurance/Reinsurance",
"Life Sciences", "Logistics", "Mining & Metals", "Other Manufacturing",
"Other Non-Manufacturing", "Retail & Wholesale", "Services (Non-Financial)", "Transportation Equipment"), class = "factor"), Q8 = c(NA,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L, 1L, 1L, NA, 1L, NA, 2L, NA, 1L)), row.names = c(NA,
-94L), class = c("data.table", "data.frame"), .internal.selfref = <pointer:
0x0000000000090788>, .Names = c("Q7_1",
"Q8", "Q9", "Q10"))
var, which is"Q7_1") to functions likemean, which expect a numeric vector.dplyrcan work with these character strings forselectand similar functions, butquantileandmeancan't.enquo/quo_nameand!!