I have a dataframe with an ID column that has multiple observations per unique ID. I also have a column that has a binary value - "commute" or "cluster". How it usually goes is about a dozen or so observations of cluster broken up by one or two observations of commute, and then back to several clusters, etc. What I want to do is create a seperate column for ID where each unique ID is broken up into several ID's based on clusters broken up by commuting.
As in, say observations 1-15 are cluster, then 16-17 is commute, then 18-30 is cluster. And these all belong to ID "1". What I want is observations 1-15 to be labelled as "1.1" and then observations 18-30 to be "1.2", etc. So each consecutive grouping of clusters is banded together in its own uniqe ID, then the next grouping of clusters also has its own unique ID, and each uniqe ID is broken up by one or more commute observations. I hope I've explained that well enough to communicate what I want.
group_by(track_ID) %>%
mutate(foraging_event = ifelse(activity3 == "commuting", NA,
cumsum(lag(activity3 == "commuting", default = TRUE)))) %>%
mutate(foraging_event_tight = ifelse(activity2 != "commuting",
NA, cumsum(lag(activity2 != "commuting", default = TRUE)))) %>%
ungroup()
This is a code we tried but did not work. Does anyone has suggestions for what we might be able to do?