Nova
Nova

Reputation: 5861

Create unique groups row wise based on logical vector in data.frame

I think there must be a solution on SO for this but I've been searching around solutions with almost what I want, but not quite. Looking for a tidyverse solution, if possible.

I have a data.frame, say newdf:

newdf <- data.frame(inside.city = c(TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE))

newdf
  inside.city
1        TRUE
2        TRUE
3        TRUE
4       FALSE
5       FALSE
6        TRUE
7       FALSE
8       FALSE

Every time someone "leaves the city" (inside.city == FALSE), I want to give their trip a unique group number, so that the resulting data.frame looks like this:

  inside.city group
1        TRUE    NA
2        TRUE    NA
3        TRUE    NA
4       FALSE     1
5       FALSE     1
6        TRUE    NA
7       FALSE     2
8       FALSE     2

Assume the data are already ordered by the date.

How can I do this efficiently?

Upvotes: 1

Views: 61

Answers (1)

MrFlick
MrFlick

Reputation: 206167

Here's a way using mutate(). I just transform the column twice to simplify things

library(dplyr)
newdf %>% mutate(group=cumsum(!inside.city & lag(inside.city, default=TRUE)), 
                 group=ifelse(inside.city, NA, group)) 

Basically you just increment when you see a FALSE after a TRUE and then set the TRUE values to NA.

Upvotes: 3

Related Questions