Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Last active January 23, 2018 13:55
Show Gist options
  • Save abikoushi/db838b6477b0b612d4da1343a0fa3f46 to your computer and use it in GitHub Desktop.
Save abikoushi/db838b6477b0b612d4da1343a0fa3f46 to your computer and use it in GitHub Desktop.
make_q_df <- function(sf,p=c(0.1,0.25,0.5,0.75,0.9)){
sf_q <-quantile(sf,probs = p)
if(is.null(sf$strata)){
q_df<-sf_q$quantile %>%
as_data_frame() %>%
set_names(paste0("q",p))
q_df[is.na(q_df)]<-Inf
}else{
q_df<-sf_q$quantile %>%
as_data_frame() %>%
set_names(paste0("q",p)) %>%
mutate(strata=rownames(sf_q$quantile))
q_df[is.na(q_df)]<-Inf
tmp <- do.call("rbind",strsplit(q_df$strata,", |="))
n_strata <- ncol(tmp)/2
values=tmp[,2*1:n_strata]
if(is.null(dim(values))){
q_df <- data.frame(q_df,values,stringsAsFactors = FALSE)
colnames(q_df)[colnames(q_df)=="values"] <- tmp[1]
}else{
colnames(values)<-tmp[1,2*1:n_strata-1]
q_df <- data.frame(q_df,values,stringsAsFactors = FALSE)
}
}
return(q_df)
}
theme_rect <- function(base_size=16,base_family="HiraKakuProN-W3"){
theme_grey(base_size = base_size, base_family = base_family) %+replace%
theme(axis.text = element_text(colour="black"),
plot.margin = unit(c(5.5,25,5.5,5.5),"pt"),
panel.grid = element_blank(),
panel.background = element_rect(fill="white"),
panel.border = element_rect(size=1,fill = NA,colour = "black"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment