Hi, I have two data-frames which I want to match by a date-variable. The problem now is, that the date-variable has a different format in this two data-frames. So simply matching by this variable wouldn't work. In the one data-frame the date-variable contains strings that look like this: 14.Aug.2012-16:32 3.Sep.2012-16:50 etc. in the other data-frame the date-variable looks like this: 2012.08.14 2012.09.3 is there an easy way to compare these two vectors? Or is there an easy way to transform 14.Aug.2012-16:32 into 2012.08.14 ? thanks!
Hello,
Try the following.
x <- c("14.Aug.2012-16:32", "3.Sep.2012-16:50")
y <- c("2012.08.14", "2012.09.3")
as.Date(x, format = "%d.%b.%Y-%H:%M") == as.Date(y, format =
"%Y.%m.%d")
(Or use %in% instead of ==)
But note that the format returned by as.Date uses '-' as separator:
as.Date(x, format = "%d.%b.%Y-%H:%M")
[1] "2012-08-14" "2012-09-03"
Hope this helps,
Rui Barradas
Em 14-09-2012 10:44, Martin Batholdy escreveu:> Hi,
>
> I have two data-frames which I want to match by a date-variable.
>
> The problem now is, that the date-variable has a different format in this
two data-frames.
> So simply matching by this variable wouldn't work.
>
> In the one data-frame the date-variable contains strings that look like
this:
>
> 14.Aug.2012-16:32
> 3.Sep.2012-16:50
>
> etc.
>
> in the other data-frame the date-variable looks like this:
>
> 2012.08.14
> 2012.09.3
>
>
> is there an easy way to compare these two vectors?
> Or is there an easy way to transform
>
> 14.Aug.2012-16:32
> into
> 2012.08.14 ?
>
>
> thanks!
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
HI,
Try this:
vec1<-c("14.Aug.2012-16:32","3.Sep.2012-16:50")
vec2<-c(c("2012.08.14", "2012.09.3"))
vec1DateF<-as.Date(vec1,format="%d.%b.%Y-%H:%M")
vec2DateF<-as.Date(vec2,format="%Y.%m.%d")
?ifelse(difftime(vec1DateF,vec2DateF)==0,TRUE,FALSE)
#[1] TRUE TRUE
A.K.
----- Original Message -----
From: Martin Batholdy <batholdy at googlemail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Friday, September 14, 2012 5:44 AM
Subject: [R] compare different data-formats
Hi,
I have two data-frames which I want to match by a date-variable.
The problem now is, that the date-variable has a different format in this two
data-frames.
So simply matching by this variable wouldn't work.
In the one data-frame the date-variable contains strings that look like this:
14.Aug.2012-16:32
3.Sep.2012-16:50
etc.
in the other data-frame the date-variable looks like this:
2012.08.14
2012.09.3
is there an easy way to compare these two vectors?
Or is there an easy way to transform
14.Aug.2012-16:32
into
2012.08.14 ?
thanks!
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reasonably Related Threads
- : record which entry in one file doesn't appear in a different file
- updating elements of a vector sequentially - is there a faster way?
- Finding the difference between two vectors
- slowness when I use a list comprehension
- slowness when I use a list comprehension