I have a main data frame (data
) that contains information about purchases: names, year, city, and a few other variables:
Name Year City
N1 2018 NY
N2 2019 SF
N2 2018 SF
N1 2010 NY
N3 2020 AA
I used new_data <- data %>% group by(Name) %>% tally(name = "Count")
to get something like this:
Name Count
N1 2
N2 2
N3 1
My questions, preferably using dplyr:
1) How do I now add the city that corresponds to Name to new_data
, i.e:
Name Count City
N1 2 NY
N2 2 SF
N3 1 AA
2) How do I add the earliest year of each Name to new_data, i.e.:
Name Count City Year
N1 2 NY 2010
N2 2 SF 2018
N3 1 AA 2020