Hi there,
Looking for some guidance with 'lag' and/or the capability of rolling into a list.
I want to produce a list of the last 5 results based on the name of the individual. I understand lag(result,1) will give me the last result but that's a far as I can get.
Here is my example data
data <- tibble::tribble(
~Name, ~Result,
"A", 1L,
"B", 2L,
"A", 3L,
"A", 4L,
"B", 5L,
"A", 6L,
"B", 7L,
"A", 8L,
"A", 9L,
"A", 5L
)
here is my wanted result
expected <- tibble::tribble(
~Name, ~Result, ~Expected,
"A", 1L, NA,
"B", 2L, NA,
"A", 3L, "1",
"A", 4L, "1,3",
"B", 5L, "2",
"A", 6L, "1,3,4",
"B", 7L, "2,5",
"A", 8L, "1,3,4,6",
"A", 9L, "1,3,4,6,8",
"A", 5L, "3,4,6,8,9"
)
Any guidance would be appreciated!
Regards,
M