Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New function to evaluate any in i grouped by keyby or by #6661

Open
tsp opened this issue Dec 12, 2024 · 3 comments
Open

New function to evaluate any in i grouped by keyby or by #6661

tsp opened this issue Dec 12, 2024 · 3 comments

Comments

@tsp
Copy link

tsp commented Dec 12, 2024

Hi
Would it be possible to add a function that evaluates any() in i by each group given by keyby or by. So instead of

DT<-data.table(Id=rep(1:3,each=3),Trt=c("A","A","B","C","B","B","A","A","A"),Dur=1:9)
DT[Id %in% DT[Trt=="A",Id],sum(Dur),by=Id]

the final line would be replaced by
DT(anyby(Trt=="A"),sum(Dur),by=Id)

@rikivillalba
Copy link
Contributor

These forms are equivalent

DT[Id %in% Id[Trt=="A"],sum(Dur),by=Id]
DT[, .SD[any(Trt=="A"), .(sum(Dur))], by = Id]

The last one is particularly close to your use case. Note the .(), if you omit it , you'll obtain zero for the sum of id's with no A's in Trt

@MichaelChirico
Copy link
Member

MichaelChirico commented Dec 12, 2024

IIUC #788 solves this:

DT[, by=Id, having=any(Trt=='A'), sum(Dur)]

In current code, slightly more canonical (readable?) than the above suggestion with .SD is

DT[, if(any(Trt=='A')) sum(Dur), by=Id]

@Kamgang-B
Copy link
Contributor

Another way, likely more useful when Trt contains NAs (parhaps more readable), is:

DT[, if("A" %in% Trt) sum(Dur), by=Id]

The (current) approach suggested by @MichaelChirico would fail if there is a group (in Id) that contains only NAs, or only NAs and values different from "A"; See data below (modified):

DT <- data.table(Id=rep(1:3,each=3),Trt=c("A","A","B","C","B","B","B",NA,NA),Dur=1:9)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants