I am very new to R (coming from many years of using SAS). and I am trying to understand some of the functions in R.
I am very much confused by lead and lag functions in dplyr. In the following code, I was expecting them to generate the same result as first and last dates as I am grouping by id then arranging by visit date
df <- data.frame(ID = rep(1:10, each = 4) ,
Visit_Date = as.Date(sample(1:365, 10, replace=TRUE), origin=“1970-01-01”),
stringsAsFactors = TRUE)
df2 <- data.frame(df %>%
mutate(last_visit = lag(Visit_Date)) %>%
Could anyone explain this for me please?