0

I want to create a function used for data.table. Supposed we have

library(data.table)    
DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)

    foo <- function(data, field, grp){
      data[, field, by=grp]
    }

I have tried

   foo(DT, .N, grp = y)

or

   foo(DT, y)

They return errors. How do I pass input arguments in the data.table?

2
  • The function already exists, like data.table:::`[.data.table`(DT, j=.N, by=y). I doubt that you need to use it like this, though. What does it gain you to use foo rather than the []s? Commented Apr 25, 2016 at 6:29
  • @Frank I am trying to call DTs with the same by. I am wondering if we could create function to help it. Commented Apr 25, 2016 at 6:38

1 Answer 1

1

You are in essence asking to reinvent the function [.data.table. The [ function perform an implicit evaluation of the second argument, j, in the context of the datatable. In the case of getting counts by groups it's just:

DT[ ,.N, by=y]
   y N
1: 1 3
2: 3 3
3: 6 3

Had you wanted sequences by groups it could have been:

> DT[ ,1:.N, by=y]
   y V1
1: 1  1
2: 1  2
3: 1  3
4: 3  1
5: 3  2
6: 3  3
7: 6  1
8: 6  2
9: 6  3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.