#' Transform census variables to long form
#' where variable names are concatinated descriptor & two digit decade integer
#' @export
demog_long = function(d = df, t = t_var, ids = id, vars = demographics){
t.i = unique(d[ ,t])
t.i.abrev = substr(t.i, nchar(t.i) - 1, nchar(t.i))
n = nrow(d)
p = length(demographics) / length(t.i) + 2
d.out = as.data.frame(mat.or.vec(nr = n, nc = p))
for (j in 1:length(t.i)){
d.t = d[ d[,t] == t.i[j], ]
t_set = t.i.abrev[j]
if (t_set == '10'){
t_set = paste(t_set, '0a', sep = '|')
}
t_var_indices = grep(t_set, vars)
v.set = d.t[ ,vars][ ,t_var_indices]
v.names = colnames(v.set)
common_names = gsub(t_set, "", v.names)
v.set.out = cbind(v.set, d.t[ ,c(t, ids)])
colnames(v.set.out) = c(common_names, t, ids)
n_d.t = nrow(d.t)
rows = (((j - 1) * n_d.t) + 1):(j * n_d.t)
d.out[rows,] = v.set.out
colnames(d.out) = colnames(v.set.out)
}
return(d.out)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.