Skip to content

Instantly share code, notes, and snippets.

@brodieG
Last active September 22, 2021 12:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brodieG/85271ef3de82509ad036 to your computer and use it in GitHub Desktop.
Save brodieG/85271ef3de82509ad036 to your computer and use it in GitHub Desktop.
DT[
id %in% 200:300,
.(reg.val = sum(value)),
by=region
][
reg.val > 0,
range(reg.val)
]
@jangorecki
Copy link

Another good option, useful for longer chains:

DT[id %in% 200:300, .(reg.val = sum(value)), by=region
   ][reg.val > 0, range(reg.val)
     ]

Each data.table query in a separate row.

@nickleeh
Copy link

@jangorecki

"Each data.table query in a separate row." -- Good point!

I add one space before and after the square brackets, which may arguablly improve the readability a little bit. (the ][ pair loods weird if they stick together):

DT [ id %in% 200:300, .(reg.val = sum(value)), by= region
   ] [ reg.val > 0, range(reg.val) ]

(Also put a space after by=, which I consider as a key word.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment