Hossein
Hossein

Reputation: 19

How to select date among several similar date in a column in R

I have a column like: NA NA NA NA NA NA NA
[8] NA NA NA NA NA NA NA
[15] NA NA NA NA NA NA NA
[22] NA NA NA NA NA NA NA
[29] NA NA NA NA NA NA NA
[36] NA NA NA NA NA "1990-12-31" "1990-12-31" [43] "1990-12-31" "1990-12-31" "1990-12-31" "1990-12-31" "1990-12-31" "1990-12-31" "1990-12-31" [50] "1990-12-31" "1990-12-31" "2001-12-31" "2001-12-31" "2001-12-31" "2001-12-31" "2001-12-31" [57] "2001-12-31" "2001-12-31" "2001-12-31" "2001-12-31" "2001-12-31" "2001-12-31" "2012-12-31" [64] "2012-12-31" "2012-12-31" "2012-12-31" "2012-12-31" "2012-12-31" "2012-12-31" "2012-12-31" [71] "2012-12-31" "2012-12-31" "2012-12-31" NA NA NA NA
[78] NA NA NA

I want to save a data frame in R like: Date "1990-12-31" "2001-12-31" "2012-12-31"

is there anyone to help me. I used the below code but it doesn't worked:

 total_flow_annuall$Max_date%>% select(which(sapply(.,is.Date)))

Upvotes: 0

Views: 21

Answers (1)

Taufi
Taufi

Reputation: 1577

The following is a short Base R option that illustrates the solution with an example vector. The idea is to identify all unique elements that are not NA. No need to check for dates as there are no other types of values present apart from NA.

Code:

example <- c(NA, NA, "1990-12-31", "1990-12-31", NA, "1991-12-31")
date <- unique(example[!is.na(example)])

Output:

[1] "1990-12-31" "1991-12-31"

Application:

Given the code line you provided, the line

date <- unique(total_flow_annuall$Max_date[!is.na(total_flow_annuall$Max_date)])

should probably work for you.

Upvotes: 1

Related Questions