Quantcast

Dates in R

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Dates in R

BrittD
Hi everyone,

I have a file in which the dates are subscribed as for instance: 20101020.
This is 20th Octobre 2010.
My problem is that R won't except this as a date, since there is no sign to seperate the Year, Month and Day
and that it will only see it as an origin, which it is not.
Does anyone know what to do about this.

Greetings,

Britt
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Dates in R

Rui Barradas
Hello,

Try

x <- 20102010

as.Date(as.character(x), format="%Y%d%m")
[1] "2010-10-20"
as.POSIXct(as.character(x), format="%Y%d%m")
[1] "2010-10-20 BST"

Note that you must pass x as a character vector.
If not, the date functions will see it as the number of days since an origin
such as 1970-01-01 or others. See the help page for as.POSIXct for examples on this.

?as.POSIXct

This is obviously not your case, so the examples above should solve it.
For date formats see

?strptime

Hope this helps,

Rui Barradas
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Dates in R

Rui Barradas
Oops,

Sorry, wrong number.

x <- 20101020

as.Date(as.character(x), format="%Y%m%d")
as.POSIXct(as.character(x), format="%Y%m%d")

Rui Barradas
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Dates in R

Uwe Ligges-3
In reply to this post by BrittD


On 07.05.2012 10:24, BrittD wrote:
> Hi everyone,
>
> I have a file in which the dates are subscribed as for instance: 20101020.
> This is 20th Octobre 2010.

strptime("20101020", format="%Y%m%d")

seems to work for me...

UWe Ligges


> My problem is that R won't except this as a date, since there is no sign to
> seperate the Year, Month and Day
> and that it will only see it as an origin, which it is not.
> Does anyone know what to do about this.
>
> Greetings,
>
> Britt
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Dates-in-R-tp4614266.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Loading...