Quantcast

factor conversion to date/time

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

factor conversion to date/time

mpostje




Hi, I've been trying to convert numbers from an online temperature database
into dates and time that R recognizes. I've tried as.Date, as.POSIXlt and strptime  the problem is that the database has put a T between the numbers and R will not accept any conversions. currently it sees the date as a factor with the format  as 1981-01-02T08:00I would like to keep only the year and month, but my primary focus is to get R to recognize it as a date.

Thank you for your help. cheers,Marjolein          
        [[alternative HTML version deleted]]

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: factor conversion to date/time

Michael Weylandt
Please don't triple post.

Michael

On Wed, May 2, 2012 at 8:08 AM, marjolein post <[hidden email]> wrote:

>
>
>
>
> Hi, I've been trying to convert numbers from an online temperature database
> into dates and time that R recognizes. I've tried as.Date, as.POSIXlt and strptime  the problem is that the database has put a T between the numbers and R will not accept any conversions. currently it sees the date as a factor with the format  as 1981-01-02T08:00I would like to keep only the year and month, but my primary focus is to get R to recognize it as a date.
>
> Thank you for your help. cheers,Marjolein
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: factor conversion to date/time

Duncan Murdoch-2
In reply to this post by mpostje
On 02/05/2012 8:08 AM, marjolein post wrote:
>
>
>
> Hi, I've been trying to convert numbers from an online temperature database
> into dates and time that R recognizes. I've tried as.Date, as.POSIXlt and strptime  the problem is that the database has put a T between the numbers and R will not accept any conversions. currently it sees the date as a factor with the format  as 1981-01-02T08:00I would like to keep only the year and month, but my primary focus is to get R to recognize it as a date.

To stop the conversion to factors, use argument stringsAsFactors=FALSE
when you read the file, or even better, options(stringsAsFactors=FALSE)
for a global change.

To convert a string, specify the format (following the instructions in
?strptime):

x <- "1981-01-02T08:00I"
strptime(x, format="%Y-%m-%dT%H:%MI")

Duncan Murdoch

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: factor conversion to date/time

John Kane
In reply to this post by mpostje
Try something like this.  Convert the vector to character and grab the first 10 characters then convert to a date.

aa <- as.factor("1981-01-02T08:00I")
aa <- as.character(aa)
aa <- substr(aa, 1, 10)
class(b)

John Kane
Kingston ON Canada


> -----Original Message-----
> From: [hidden email]
> Sent: Wed, 2 May 2012 14:08:26 +0200
> To: [hidden email]
> Subject: [R] factor conversion to date/time
>
> Hi, I've been trying to convert numbers from an online temperature
> database
> into dates and time that R recognizes. I've tried as.Date, as.POSIXlt and
> strptime  the problem is that the database has put a T between the
> numbers and R will not accept any conversions. currently it sees the date
> as a factor with the format  as 1981-01-02T08:00I would like to keep only
> the year and month, but my primary focus is to get R to recognize it as a
> date.
>
> Thank you for your help. cheers,Marjolein
> [[alternative HTML version deleted]]
>

____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!

______________________________________________
[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...