HappyMan
HappyMan

Reputation: 75

Keep empty cell when selecting first occurence of dates in R

to select the 'first' occurence of a date column in R I use sometimes a code like this:

data %>%
group_by(id) %>%
arrange(eventdate) %>%
slice(1L)

The problem : it's taking the first occurence of the date even if there is an empty cell before it.

what is the best solution to add a filter or a function that prints that empty /eventdate/ cell and not jump to the next eventdate cell ?

Thanks

Upvotes: 0

Views: 44

Answers (1)

Hong
Hong

Reputation: 594

If the problem is that eventdate column contains NA and that is forcing those rows to the bottom of each 'group', you can try this:

library(dplyr)

data %>%
group_by(id) %>%
arrange(!is.na(eventdate), eventdate) %>%
slice(1L)

Upvotes: 1

Related Questions