Skip to content

Instantly share code, notes, and snippets.

@ibarraespinosa
Created January 7, 2019 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibarraespinosa/88ad8f4e194ed6631102eae575852a00 to your computer and use it in GitHub Desktop.
Save ibarraespinosa/88ad8f4e194ed6631102eae575852a00 to your computer and use it in GitHub Desktop.
# The 'keyed' data.table DT
DT <- data.table(A = letters[c(2, 1, 2, 3, 1, 2, 3)],
B = c(5, 4, 1, 9, 8, 8, 6),
C = 6:12)
setkey(DT, A, B)
# Select the "b" group
DT["b"]
# "b" and "c" groups
DT[c("b", "c")]
# The first row of the "b" and "c" groups
DT[c("b", "c"), mult = "first"]
# First and last row of the "b" and "c" groups
DT[c("b", "c"), .SD[c(1, .N)], by = .EACHI]
# Copy and extend code for instruction 4: add printout
DT[c("b", "c"), { print(.SD); .SD[c(1, .N)] }, by = .EACHI]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment