Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9227313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:02:36+00:00 2026-06-18T05:02:36+00:00

Any help with this would be really appreciated. I am using the Lumley survey

  • 0

Any help with this would be really appreciated. I am using the Lumley survey package and am trying to simplify my code, but have hit a slight snag.

The svymean function from the package is called as follows in my code, where the first argument is a formula indicating which variables I want, and the second argument is that dataset:

svymean(~hq_ehla, FraSvy, na.rm=TRUE)

I’m trying to create a function that will pull out the mean (proportions) and standard errors for categorical variables, so I’ve made the following function:

stats <- function(repstat, num) {
    estmean <- as.numeric(round(100 * repstat[num], digits=0))
    estse <- round(100 * sqrt(attributes(repstat)$var[num,num]), digits=1)
    return(list(mean=estmean, se=estse))
}

This works, so when I’m pulling out the mean and se of my first category, for example, I use:

stats(svymean(~hq_ehla, FraSvy, na.rm=TRUE), 1)$mean
stats(svymean(~hq_ehla, FraSvy, na.rm=TRUE), 1)$se

What I’d like to be able to do is simplify this to something much shorter, where maybe I’d only have to write:

stats(FraSvy, "hq_ehla", 1)$mean

Or something like that. Problem is that I can’t figure out how to pass a formula to a function using a variable name.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:02:37+00:00Added an answer on June 18, 2026 at 5:02 am

    You can use reformulate to construct your formula and call svymean within your function. Use ... to pass na.rm or other arguments to svymean

    stats <- function(terms, data,  num, ...) {
      .formula <- reformulate(terms)
      repstat <- svymean(.formula, data, ...)
      estmean <- as.numeric(round(100 * repstat[num], digits=0))
      estse <- round(100 * sqrt(attributes(repstat)$var[num,num]), digits=1)
      return(list(mean=estmean, se=estse))
    }
    
    stats(data = FraSvy, terms = "hq_ehla", 1, na.rm = TRUE)$mean
    

    Have a look at this answer for more details on programmitically create formula objects

    Or, you could pass a formula object within the function.

    stats2 <- function(formula, data,  num, ...) {
    
      repstat <- svymean(formula, data, ...)
      estmean <- as.numeric(round(100 * repstat[num], digits=0))
      estse <- round(100 * sqrt(attributes(repstat)$var[num,num]), digits=1)
      return(list(mean=estmean, se=estse))
    }
    
    
    stats2(data = FraSvy, formula = ~hq_ehla, 1, na.rm = TRUE)$mean
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Any help here would be greatly appreciated. I have this table hospital Nurse |
I'm relatively new to Java and would appreciate any help on this! I have
am trying to squeeze some performance out of this query, any help would be
I have been trawling google for answers to this but no luck. Any help
Any help with this problem would be fantastic. I appreciate all contributions! Let us
I would appreciate any help on this issue. Lets say I want to load
By any chance you would be able to help optimize this query without me
I can't seem to find any help on this but under the Net tab
Any help most appreciated, this is driving me insane. I'm doing exactly what I
Would really appreciate some help with a search angine I'm trying to make for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.