DRAFT utility to use formulas provided as text, to calculate indicators
Source:R/calc_ejam.R, R/utils_calc_ejam.R
calc_ejam.RdDRAFT utility to use formulas provided as text, to calculate indicators
DRAFT utility to use formulas provided as text, to calculate indicators
Value
data.frame of calculated variables one row per bg row
data.frame of calculated variables one row per bg row
Details
custom_doaggregate()may usecalc_ejam()calc_ejam()usescalc_byformula()calc_byformula()usescalc_varname_from_formula()and maybe source_this_codetext()
custom_doaggregate()may usecalc_ejam()calc_ejam()usescalc_byformula()calc_byformula()usesformula_varname()and maybe source_this_codetext()
Examples
if (FALSE) { # \dontrun{
### example using just 10 blockgroups from 1 county in Delaware
c1 <- fips2countyname(fips_counties_from_state_abbrev('DE'), includestate = FALSE)[1]
bgdf = data.frame(EJAM::blockgroupstats[ST == "DE" & countyname == c1, ])[1:10, ]
newdf <- calc_ejam(bgdf, keep.old = "",
formulas = c(
"my_custom_recalc_demog <- (pctlowinc + pctmin)/2",
"mystat2 = 100 * pctlowinc"))
cbind(Demog.Index = bgdf$Demog.Index, newdf, pctlowinc = bgdf$pctlowinc)
newdf <- calc_ejam(bgdf, formulas = formulas_d)
newdf
## example of entire US
#
newdf1 <- calc_ejam(as.data.frame(bgdf), formulas = formulas_d)
t(summary(newdf1))
bgdf <- data.frame(blockgroupstats)
newdf <- calc_ejam(bgdf,
keep.old = c('bgid', 'pop', 'hisp'),
keep.new = "all",
formulas = formulas_d
)
round(t(newdf[1001:1002, ]), 3)
cbind(
newdf[1001:1031, c('hisp', 'pop', 'pcthisp')],
check = (newdf$hisp[1001:1031] / newdf$pop[1001:1031])
)
## note the 0-100 percentages in blockgroupstats versus the 0-1 calculated percentages
cbind(round(sapply(newdf, max, na.rm=TRUE),2),
names(newdf) %in% names_pct_as_fraction_blockgroupstats)
EJAM:::calc_varname_from_formula(formulas_d)
rm(bgdf)
} # }
if (FALSE) { # \dontrun{
### example using just 10 blockgroups from 1 county in Delaware
c1 <- fips2countyname(fips_counties_from_state_abbrev('DE'), includestate = FALSE)[1]
bgdf = data.frame(EJAM::blockgroupstats[ST == "DE" & countyname == c1, ])[1:10, ]
newdf <- calc_ejam(bgdf, keep.old = "",
formulas = c(
"my_custom_recalc_demog <- (pctlowinc + pctmin)/2",
"mystat2 = 100 * pctlowinc"))
cbind(Demog.Index = bgdf$Demog.Index, newdf, pctlowinc = bgdf$pctlowinc)
newdf <- calc_ejam(bgdf, formulas = formulas_d)
newdf
## example of entire US
#
newdf1 <- calc_ejam(as.data.frame(bgdf), formulas = formulas_d)
t(summary(newdf1))
bgdf <- data.frame(blockgroupstats)
newdf <- calc_ejam(bgdf,
keep.old = c('bgid', 'pop', 'hisp'),
keep.new = "all",
formulas = formulas_d
)
round(t(newdf[1001:1002, ]), 3)
cbind(
newdf[1001:1031, c('hisp', 'pop', 'pcthisp')],
check = (newdf$hisp[1001:1031] / newdf$pop[1001:1031])
)
## note the 0-100 percentages in blockgroupstats versus the 0-1 calculated percentages
cbind(round(sapply(newdf, max, na.rm=TRUE),2),
names(newdf) %in% names_pct_as_fraction_blockgroupstats)
EJAM:::formula_varname(formulas_d)
rm(bgdf)
} # }