I'd like to have a table of the first purchase date of Fruits bought by each customer.
I have attempted to answer it. However, the result is not correct. If anybody could come up with the correct result without using "arrange" function [I have used it in my code] that would be such a great help. Thank you
Data:
library(tidyverse)
sample_1 <- tibble::tribble(
~Purchased_Date, ~Customer_ID, ~Fruits,
"24/02/2017", 123L, "Apple",
"24/02/2017", 123L, "Apple",
"3/10/2016", 124L, "Banana",
"30/09/2016", 124L, "Apple",
"25/10/2016", 124L, "Apple",
"13/10/2016", 125L, "Apple",
"20/10/2016", 125L, "Apple",
"4/05/2017", 125L, "Apple",
"15/05/2017", 125L, "Banana",
"18/04/2017", 125L, "Banana",
"18/04/2017", 125L, "Banana",
"18/04/2017", 125L, "Banana"
)
My Attempt:
library(tidyverse)
sample_1 %>%
distinct() %>%
arrange(Purchased_Date, Customer_ID, Fruits) %>%
group_by(Customer_ID, Fruits) %>%
slice(1L)
Expected output:
library(tidyverse)
expected_output <- tibble::tribble(
~Purchased_Date, ~Customer_ID, ~Fruits,
"24/02/2017", 123L, "Apple",
"3/10/2016", 124L, "Banana",
"30/09/2016", 124L, "Apple",
"13/10/2016", 125L, "Apple",
"15/05/2017", 125L, "Banana"
)
As you can see my attempt output is not matching the expected output since something happens with arrange function and doesn't give correct output.
I am hoping to have a solution using tidyverse and without using arrange function.