I am trying to create a sequence along two different parameters about how people have moved from one location to another. I have the following information
name<- c("John", "John", "John", "Sam","Sam", "Robert", "Robert","Robert")
location<- c("London", "London", "Newyork", "Houston", "Houston", "London", "Paris","Paris")
start_yr<- c(2012, 2012, 2014, 2014, 2014,2012,2013, 2013)
end_yr<- c(2013, 2013, 2015, 2015, 2015, 2013, 2015, 2015)
df<- data.frame(name,location,start_yr, end_yr)
I need to seq_along the name and location and create a transition variable of year to know if this person has moved in that year or not. I tried this but it didn't work very well. I was getting strange years meaning the name column sometimes doesn't start with 1. Any suggestions on how to approach this problem?
ave(df$name,df$location, FUN = seq_along)
I would like to have
name location move year
John London 1 2012
John London 0 2013
John Newyork 1 2014
John Newyork 0 2015