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

  • Home
  • SEARCH
  • 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 8187143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:27:58+00:00 2026-06-07T02:27:58+00:00

Many R functions take a variable number of arguments. sum() is an example: sum(1,

  • 0

Many R functions take a variable number of arguments. sum() is an example: sum(1, 2), sum(1, 2, 3), and sum(1, 2, 3, 4) are all valid commands.

I need to write scripts that run in batch mode. In these scripts, I need to pass multiple arguments to a function. The arguments will not be passed in from the command line. They must be variables (not strings corresponding to variable names). They will all be of the same class, and their names will start with the same characters, but I won’t know the names or the number of arguments. Is there a succinct way to pass the variables to the function?

Here is an example: I want code the yields the sum of all variables whose names have the pattern ^int\\d$. I know that there is at least one such variable, but I don’t know how many there are. This code works:

# Set up toy data
int1 <- 3
int2 <- 5

# Get the sum
argNames <- ls(pat='^int\\d$')
argNames.list <- as.list(argNames)
argNames.list <- lapply(argNames.list, function (x) get(x))
do.call(sum, argNames.list)

My objection is that this code is a little cumbersome. Spreading the operation out over four lines reduces clarity. Is there an R-idiomatic way to get the same result with fewer lines of code?

  • 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-07T02:28:00+00:00Added an answer on June 7, 2026 at 2:28 am

    (Edited to better address the question)

    I don’t think you can do much better than this, which seems pretty minimal to me.

    int1 <- 3
    int2 <- 5
    
    do.call(sum, lapply(ls(pat="^int\\d$"), get))
    # [1] 8
    
    ## Or use a safer version that will only look for objects located in the 
    ## environment from which it was called.  
    do.call(sum, lapply(ls(pat="^int\\d$"), get, envir=parent.frame(), inherits=FALSE))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Many numpy functions take dtype arguments as either strings (like float64 ) or numpy
Many functions in c take pointer to constant strings/chars as parameters eg void foo(const
I have a js variable with many functions, and I want to create in
I do not use that many functions but when I do I tend to
I find that many high level functions are missing in most well-known javascript libraries
Most examples of the use of fixed point combinators involve functions that take integers
I have a multidimensional array that contains a variable number of sub arrays. Each
I need to be able to take a formula that uses the OpenDocument formula
I'm porting an old 32-bit COM server to 64-bit architecture. Many functions take buffer
Let's say for example that I have two functions with random code inside and

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.