Skip to content

Instantly share code, notes, and snippets.

@ibarraespinosa
Created January 7, 2019 09:31
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/8a842d39032643f081530275f87cb20b to your computer and use it in GitHub Desktop.
Save ibarraespinosa/8a842d39032643f081530275f87cb20b to your computer and use it in GitHub Desktop.
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,
key = "A,B")
# Se puede etear los KEYS en la definicion de DT
@ibarraespinosa
Copy link
Author

ibarraespinosa commented Jan 7, 2019

# 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, 
                 key = "A,B")

# Print the sequence (-2):10 for the "b" group
DT[.("b", (-2):10)]

# Add code: carry the prevailing values forwards
DT[.("b", (-2):10), roll = TRUE]

# Add code: carry the first observation backwards
DT[.("b", (-2):10), roll = TRUE, rollends = TRUE]
DT[.("b", (-2):10), roll = TRUE, rollends = c(TRUE, TRUE)] # also OK

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