Skip to content

Instantly share code, notes, and snippets.

@tdsmith
Created August 5, 2014 00:38
Show Gist options
  • Save tdsmith/0adfb4aabf59e0ba40c3 to your computer and use it in GitHub Desktop.
Save tdsmith/0adfb4aabf59e0ba40c3 to your computer and use it in GitHub Desktop.
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