1
votes

Data based on the dataset from Kaggle here and extracted to R.

Using the following structure:

Index   VisitorId           VisitId     Visit# Hit# pagePath
0       000722514342430295  1470093727  1      1    /home
1       000722514342430295  1470093727  1      3    /google+redesign/apparel
2       000722514342430295  1470093727  1      4    /asearch.html
3       000722514342430295  1470093727  1      5    /asearch.html
4       0014659935183303341 1470037282  1      1    /home
5       0015694432801235877 1470043732  1      1    /home
6       0015694432801235877 1470043732  1      2    /google+redesign/electronics
7       0015694432801235877 1470043732  1      3    /google+redesign/apparel/men++s/men++s+t+shirts
8       0015694432801235877 1470043732  1      4    /google+redesign/apparel/kid+s/kid+s+infant
9       0015694432801235877 1470043732  1      5    /google+redesign/apparel/kid+s/kid+s+infant/quickview

I'm trying to implement a mutate lag function which will return the previous pagepath for a given visit by a given visitor.

For example, new column prev_path would be both visitorid and visitid specific and would lag Hit# by 1 but would return an <NA> when not available in the case of Visit 1, Hit 2.

2
so the way to do conditional mutates is to do mutate(ifelse(condition, do this is true, do this if false)). I'd write the specific code but don't fully understand the condition, could you be more specific pls? Ideally you can write out your expected output dataframe - Amit Kohli
So, first of all: What is your question? Did you try to implement this yourself? Which problems occurred when you tried to implement this? Stackoverflow is not a coding service, but a site for help with coding problems. Currently it is not clear from your question (and the phrasing) what exactly you are trying to achieve. Re-read your question and try rephrasing it as a coding problem. Check out what not to ask which will help you to provide a better question. - Oliver

2 Answers

2
votes

Is this what you're trying to do?

library(dplyr)

df %>%
  group_by(VisitorId, VisitId) %>%
  mutate(prev_path = ifelse(lag(`Hit#`) == `Hit#` - 1, lag(pagePath), NA))
1
votes

We can do a group_by option

library(dplyr)
df1 %>%
   group_by(VisitorId, VisitId) %>%
   mutate(prev_path = case_when(lag(`Hit#`) == `Hit#` -1 ~ lag(pathPath), 
            TRUE ~ NA_integer_))