salman baqri
salman baqri

Reputation: 1

R-how to match these two strings?

I have two columns of date strings with different sizes.

Suppose one column has strings of the form, a="3 Feb 2012" and other of the form b="Feb 3,2012"

I need entries which have the same date in both columns.

Upvotes: 0

Views: 26

Answers (1)

akrun
akrun

Reputation: 886968

We can convert to Date class using the format showed in the strings.

a1 <- as.Date(a, '%d %b %Y')
a1
#[1] "2012-02-03"

b1 <- as.Date(b, '%b %d,%Y')
b1
#[1] "2012-02-03"

and then use either == or %in% or match depending upon the need

a1 == b1
#[1] TRUE

Or another option is from lubridate package

library(lubridate)
dmy(a)== mdy(b)
#[1] TRUE

Upvotes: 3

Related Questions