Created
August 5, 2014 00:38
-
-
Save tdsmith/0adfb4aabf59e0ba40c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df = data.frame(id=1:20) | |
flags = numeric(20) | |
flags[c(5,7)] = 1 | |
df$flag = flags | |
to_copy = numeric(20) * NA | |
ptr = 1 | |
for (i in seq_along(df$id)) { | |
if(!df[i, "flag"]) next | |
candidates = c((i-5):(i-1), (i+1):(i+5)) | |
candidates = candidates[candidates >= 1 & candidates <= NROW(df)] | |
to_copy[ptr:(ptr+length(candidates)-1)] = candidates | |
ptr = ptr + length(candidates) | |
} | |
to_copy = to_copy[!is.na(to_copy)] | |
reduced = df[to_copy,] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment