I have test data that looks like this:
Group Value
1 a 1
2 a 2
3 a 3
4 a 4
5 b 5
6 b 2
7 b 3
8 c 6
9 c 7
10 c 8
11 c 3
12 c 6
13 d 9
14 d 10
15 e 9
I am trying to create a vectorized approach, preferably using tidyverse
tools that will create an additional column that notes if the Value is present in the previous grouping. Here is an example of how this would look like:
Group Value In_Last_Group
1 a 1 FALSE
2 a 2 FALSE
3 a 3 FALSE
4 a 4 FALSE
5 b 5 FALSE
6 b 2 TRUE
7 b 3 TRUE
8 c 6 FALSE
9 c 7 FALSE
10 c 8 FALSE
11 c 3 TRUE
12 c 5 TRUE
13 d 9 FALSE
14 d 10 FALSE
15 e 9 TRUE
I have a way to do this using a standard for loop, but I have a large dataset and I believe it would be much faster if it was vectorized. Any help would be appreciated.
Here is the dput
of the test data:
structure(list(Group = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 5L), .Label = c("a", "b", "c", "d",
"e"), class = "factor"), Value = c(1, 2, 3, 4, 5, 2, 3, 6, 7,
8, 3, 6, 9, 10, 9)), .Names = c("Group", "Value"), row.names = c(NA,
-15L), class = "data.frame")