R Looping through in survey package
library(survey)
data(api)
dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
# run a simple example svychisq() function
svychisq( ~sch.wide+stype , dclus1 )
# create a function that requires a character string (containing the variables)
# and the design object and runs the svychisq()
# on the contents of that character string's columns
scsloop <- function( vars , design ){ svychisq( as.formula( paste0( "~" , vars ) ) , design ) }
# test it out
scsloop( "sch.wide+stype" , dclus1 )
scsloop( "sch.wide+comp.imp" , dclus1 )
# either create a character vector to run it multiple times
cols.to.chisq <- c( "sch.wide" , "comp.imp" , "stype" )
# or identify them based on column number, if you prefer
cols.to.chisq <- names( apiclus1 )[ c( 2 , 16 , 17 ) ]
# find every combination of that vector, taken two at a time
combos <- combn( cols.to.chisq , 2 )
# separate them by +
col.combos <- paste( combos[ 1 , ] , combos[ 2 , ] , sep = "+" )
# run it on each of those character strings (print to screen and save to list)
( x <- lapply( col.combos , scsloop , dclus1 ) )
# just for kicks, print everything to the screen
col.combos[1] ; x[[1]]
col.combos[2] ; x[[2]]
col.combos[3] ; x[[3]]
Looping through columns and printing proportion or mean table based on condition with survey package
I am not very sure why the bquote is giving you so much problem, but if I use formula, it works ok:
for(i in seq_along(cols)){
FORMULA= as.formula(paste("~",cols[i]))
ifelse(type[i]=="prop",
print(prop.table(svytable(FORMULA, design))),
print(svymean(FORMULA, design))) }
var1
2 3 4
0.00000000 0.95238095 0.04761905
mean SE
var2 6.0476 0.0786
Loop though columns and print tables using survey weights from survey package
Acknowledging the use of bquote
as a partial substitution in expression from Ben's comment above, you could modify your for loop
as follows:
cols <- c("var1", "var2") # columns to loop through
for(i in seq_along(cols)){
print(prop.table(svytable(bquote(~.(as.name(cols[i]))), design)))
}
# var1
# 2 3 5
# 0.00000000 0.95238095 0.04761905
# var2
# 2 6 7
# 0.00000000 0.95238095 0.04761905
Data
library(survey)
dat <- data.frame(id=c(1,2,3), weight=c(0,2,0.1), var1=c(2,3,5), var2=c(2,6,7))
design <- svydesign(id=~1, weights=~weight, data=dat)
Looping through dataset variables in svy package
You need to use as.formula
to paste the appropriate columns for evaluation. I created a custom function for your case:
mysvy <- function(data, columns, ...) {
model <- lapply(as.list(columns), function(x) {
summary(svyglm(as.formula(paste0(names(data)[x], "~ell+meals+mobility")),
data = data, ...))
})
return(model)
}
Then you can run your your desired columns through the function.
# To run columns 13 - 15 and get the results into a list
results <- mysvy(apistrat, 13:15, design = dstrat)
# should return a list of 3. results[[1]] to see the first
Scoring a survey in R (loop)
By using dplyr
(I let Id as 1, 2, 3)
library(dplyr)
df %>%
group_by(EmployeeId) %>%
summarize(n = mean(Response))
EmployeeId n
<dbl> <dbl>
1 1 3.25
2 2 3.75
3 3 2.75
Iterate through groups of variables in a survey - R
There are multiple ways. If we pass as string, one option is to make use of group_by_at
which takes strings as argument
library(purrr)
library(dplyr)
library(survey)
library(srvyr)
map(c('sch.wide', 'cname'), ~
df %>%
group_by_at(vars("stype", .x)) %>%
summarise(prop = srvyr::survey_mean()))
#[[1]]
# A tibble: 6 x 4
# stype sch.wide prop prop_se
# <fct> <fct> <dbl> <dbl>
#1 E No 0.0833 0.0231
#2 E Yes 0.917 0.0231
#3 H No 0.214 0.110
#4 H Yes 0.786 0.110
#5 M No 0.32 0.0936
#6 M Yes 0.68 0.0936
#[[2]]
# A tibble: 30 x 4
# stype cname prop prop_se
# <fct> <fct> <dbl> <dbl>
# 1 E Alameda 0.0556 0.0191
# 2 E Fresno 0.0139 0.00978
# 3 E Kern 0.00694 0.00694
# 4 E Los Angeles 0.0833 0.0231
# 5 E Mendocino 0.0139 0.00978
# 6 E Merced 0.0139 0.00978
# 7 E Orange 0.0903 0.0239
# 8 E Plumas 0.0278 0.0137
# 9 E San Diego 0.347 0.0398
#10 E San Joaquin 0.208 0.0339
# … with 20 more rows
Or another option is to wrap with quos
to create a quosure list and evaluate (!!
) it in group_by
map(quos(sch.wide, cname), ~
df %>%
group_by(stype, !!.x) %>%
summarise(prop = srvyr::survey_mean()))
Related Topics
Scraping Tables on Multiple Web Pages with Rvest in R
How to Get a List of All Possible Partitions of a Vector in R
Count Consecutive True Values Within Each Block Separately
Visualizing Two or More Data Points Where They Overlap (Ggplot R)
How to Get Dimnames in Xtable.Table Output
Space Between Gpplot2 Horizontal Legend Elements
Inline Function Code Doesn't Compile
Plot Event Sequences/Event Sequences Clustering
Update an Entire Row in Data.Table in R
Convert R Dataframe from Long to Wide Format, But with Unequal Group Sizes, for Use with Qcc
How to Control Label Color Depending on Fill Darkness of Bars
Replace Every Single Character at the Start of String That Matches a Regex Pattern
Concatenate Values Across Columns in Data.Table, Row by Row
Ggplot2: Cannot Color Area Between Intersecting Lines Using Geom_Ribbon
Simple Comparing of Two Texts in R