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 8362631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:59:10+00:00 2026-06-09T11:59:10+00:00

I have a problem including a data.table operation in a function. Input arguments are

  • 0

I have a problem including a data.table operation in a function. Input arguments are the data.table name and the column/variable name.

I can refer to the data.table by using the get() command. However, using the same command for the variable name doesn’t work out. I know that get() might not be appropriate in case of the column/variable name, but I am stuck with which command to use.

EDITED: I have now included substitute() instead of get() and it still doesn’t work.

toy_example_fun <- function(d, .expr){

  .expr = substitute(.expr)

  setkey(get(d), .expr)  # ==> doesn't work

  d.agg <- get(d)[,list(sum(y), sum(v)), by=.expr]  # --> works
}

toy_example_fun("DT", x)

ALTERNATIVE: quote() –> This works. However, I am interested in a solution that works inside a function.

DT <- data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)    
d <- "DT"
variable <- quote(x)
d.agg <- get(d)[,list(sum(y), sum(v)), by=variable]  

Even though, the latter alternative works variable <- quote(x) produces an error message:

  <simpleError in doTryCatch(return(expr), name, parentenv, handler): object 'x' not found>
    <simpleError in is.scalar(val): object 'x' not found>
    <simpleError in is.data.frame(obj): object 'x' not found> 

Thanks for your help.

  • 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-09T11:59:11+00:00Added an answer on June 9, 2026 at 11:59 am

    Here you go:

    someFun <- function(d, .expr){
      group <- substitute(.expr)
      get(d)[,list(sum(y), sum(v)), by=group]
    }
    
    someFun("DT", x)
       group V1 V2
    1:     a 10  6
    2:     b 10 15
    3:     c 10 24
    
    
    someFun("DT", "x")
       x V1 V2
    1: a 10  6
    2: b 10 15
    3: c 10 24
    

    EDIT from Matthew :

    +1 to above. And/Or character column names are acceptable to by directly, too :

    someFun = function(d, col) {
        get(d)[,list(sum(y),sum(v)),by=col]
    }
    someFun("DT","x")
       x V1 V2
    1: a 10  6
    2: b 10 15
    3: c 10 24
    someFun("DT","x,y")
       x y V1 V2
    1: a 1  1  1
    2: a 3  3  2
    3: a 6  6  3
    4: b 1  1  4
    5: b 3  3  5
    6: b 6  6  6
    7: c 1  1  7
    8: c 3  3  8
    9: c 6  6  9
    

    but then someFun("DT",x) won’t work. So Adrie’s answer is more general.


    EDIT with setkeyv

    someFun <- function(d, cols){
      setkeyv(get(d), cols)
      cols <- substitute(cols)
      get(d)[,list(sum(y), sum(v)), by=cols]
    }
    
    someFun("DT", "x")
       x V1 V2
    1: a 10  6
    2: b 10 15
    3: c 10 24
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains a column that is XML data type. I
I have problem with my query on C, I’m using the oci8 driver. This
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have problem while loading data into html select when users press or click
I have problem SIMILAR to preventing form data reposting, but not quite the same
I have a database which has a table with an XML column. The XML
I have an Excel spreadsheet with a table of data in it. The columns
I have a very specific problem and i'm looking for input on a good
I have a long table with columns of schedule data that I'm loading via
I have a table including the columns (int id pkey, int group, double sortOrder)

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.