changing date format in a dataframe

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

changing date format in a dataframe

arunkumar1111
Hi

I have a data frame and i need to change the date format in it.
my dataframe

X Date
1 1/1/2009
2 2/1/2009
3 3/1/2009

I need to change it to 2009-01-01
Thanks in Advance
        Arun
Reply | Threaded
Open this post in threaded view
|

Re: changing date format in a dataframe

Milan Bouchet-Valat
Le mercredi 16 novembre 2011 à 06:12 -0800, arunkumar1111 a écrit :

> Hi
>
> I have a data frame and i need to change the date format in it.
> my dataframe
>
> X Date
> 1 1/1/2009
> 2 2/1/2009
> 3 3/1/2009
>
> I need to change it to 2009-01-01
See ?as.Date. In your case, I think you should use
df[[2]] <- as.Date(df[[2]], format="%d/%m/%Y")

This is assuming df is your dataframe, and that you're using the
day/month/year syntax.

To print it into another format, see ?format.Date

Regards

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

Re: changing date format in a dataframe

Michael Weylandt
In reply to this post by arunkumar1111
Can you dput() your data frame? There are a different few time objects
in R and details might depend which one you have. Though, if it's
printing like that, I'd guess it's actually a string that you can
convert to a Date using as.Date(, format = XXX) where you can work out
the formatting from the documentation in ?strptime.

Michael

On Wed, Nov 16, 2011 at 9:12 AM, arunkumar1111 <[hidden email]> wrote:

> Hi
>
> I have a data frame and i need to change the date format in it.
> my dataframe
>
> X Date
> 1 1/1/2009
> 2 2/1/2009
> 3 3/1/2009
>
> I need to change it to 2009-01-01
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/changing-date-format-in-a-dataframe-tp4076411p4076411.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.
Reply | Threaded
Open this post in threaded view
|

Re: changing date format in a dataframe

Mohamed Lajnef
In reply to this post by arunkumar1111
Hi,

If the dates are the same format and the same length you can try this :

dates<-c("1/1/2009","2/1/2009","3/1/2009")

dates_new<-as.Date(paste(substr(dates,1,3),substr(dates,7,8),sep="/"),format="%d/%m/%y"),
you can change the format to "%m/%d/%y"

Regards
M


Le 16/11/11 15:12, arunkumar1111 a écrit :

> Hi
>
> I have a data frame and i need to change the date format in it.
> my dataframe
>
> X Date
> 1 1/1/2009
> 2 2/1/2009
> 3 3/1/2009
>
> I need to change it to 2009-01-01
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/changing-date-format-in-a-dataframe-tp4076411p4076411.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.
>

--
####################################
Mohamed Lajnef,IE INSERM U955 eq 15#
P?le de Psychiatrie   #
H?pital CHENEVIER                  #
40, rue Mesly                      #
94010 CRETEIL Cedex FRANCE         #
[hidden email]           #
tel : 01 49 81 32 79   #
Sec : 01 49 81 32 90               #
fax : 01 49 81 30 99               #
####################################



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