Skip to content

Instantly share code, notes, and snippets.

@infotroph
Created July 27, 2017 15:11
Show Gist options
  • Select an option

  • Save infotroph/e997653e5c10b79f524eb0a6e74ae001 to your computer and use it in GitHub Desktop.

Select an option

Save infotroph/e997653e5c10b79f524eb0a6e74ae001 to your computer and use it in GitHub Desktop.
# Have: file with observation ids that reset every time the instrument is restarted
# Want: Session identifiers that increment each restart
# Base R approach shown below. Is there a "standard" Tidyverse approach to this?
dat = read.csv(text = "
id,y
RESET
1,1.2
2,3.5
3,2.8
RESET,
1,1.5
2,1.8
3,2.2
4,3.3
RESET,
1,5.4
1,1.1",
stringsAsFactors = FALSE)
starts = which(dat$id == "RESET")
dat$session = cut(
x = seq_along(dat$id),
breaks = c(starts, nrow(dat)),
labels = seq_along(starts),
right = FALSE,
include.lowest = TRUE)
dat = dat[-starts,]
dat
# id y session
# 2 1 1.2 1
# 3 2 3.5 1
# 4 3 2.8 1
# 6 1 1.5 2
# 7 2 1.8 2
# 8 3 2.2 2
# 9 4 3.3 2
# 11 1 5.4 3
# 12 1 1.1 3
@infotroph
Copy link
Author

Thanks to @nibrivia for the much cleaner Tidyverse approach: dat %>% mutate(session = cumsum(id=="RESET"), id=as.numeric(id)) %>% drop_na(id)

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