Blueberry
Blueberry

Reputation: 363

select unique values and it's corresponding values in dplyr

I have a dataset that looks like this :

enter image description here

Is there a way in dplyr to select the first observation of each group of weeknum and it's correpsonding date i know how to get the unique values but i don't know how to get their corresponding dates if you could help I would appreciate it.

Thank you in advance

Upvotes: 0

Views: 242

Answers (1)

akrun
akrun

Reputation: 887951

We can use slice_head after grouping by the 'weeknum'

library(dplyr)
df1 %>%
   group_by(weeknum) %>%
   slice_head(n = 1)

Or with distinct

df1 %>%
     distinct(weeknum, .keep_all = TRUE)

In base R, it can be done with duplicated and subset

subset(df1, !duplicated(weeknum))

Upvotes: 1

Related Questions