[R] as.date

Joshua Wiley jwiley.psych at gmail.com
Tue Jun 1 18:06:06 CEST 2010


Hello,

The lowercase 'y' is year without century.  This should work for you:

as.Date(x=df$CREATED.DATE, format="%d/%m/%Y")

HTH,

Josh


On Tue, Jun 1, 2010 at 8:57 AM, arnaud Gaboury <arnaud.gaboury at gmail.com> wrote:
> Dear group,
>
> Here is my df (obtained with a read.csv2()):
>
>
> df <-
> structure(list(DESCRIPTION = c("COTTON NO.2 Jul/10", "COTTON NO.2 Jul/10",
> "PALLADIUM Jun/10", "PALLADIUM Jun/10", "SUGAR NO.11 Jul/10",
> "SUGAR NO.11 Jul/10"), CREATED.DATE = c("13/05/2010", "13/05/2010",
> "14/05/2010", "14/05/2010", "10/05/2010", "10/05/2010"), QUANITY = c(1,
> 1, -1, -1, 1, 1), CLOSING.PRICE = c("81.2000", "81.2000", "503.6000",
> "503.6000", "13.8900", "13.8900")), .Names = c("DESCRIPTION",
> "CREATED.DATE", "QUANITY", "CLOSING.PRICE"), row.names = c(NA,
> 6L), class = "data.frame")
>
>> str(df)
> 'data.frame':   6 obs. of  4 variables:
>  $ DESCRIPTION  : chr  "COTTON NO.2 Jul/10" "COTTON NO.2 Jul/10" "PALLADIUM
> Jun/10" "PALLADIUM Jun/10" ...
>  $ CREATED.DATE : chr  "13/05/2010" "13/05/2010" "14/05/2010" "14/05/2010"
> ...
>  $ QUANITY      : num  1 1 -1 -1 1 1
>  $ CLOSING.PRICE: chr  "81.2000" "81.2000" "503.6000" "503.6000" ...
>
> I want to change the class of df$CREATED.DATE from Chr to Date:
>
>
>>pose$CREATED.DATE=as.Date(pose$CREATED.DATE,"%d/%m/%y")
>
> Here is what I get :
>
> df <-
> structure(list(DESCRIPTION = c("COTTON NO.2 Jul/10", "COTTON NO.2 Jul/10",
> "PALLADIUM Jun/10", "PALLADIUM Jun/10", "SUGAR NO.11 Jul/10",
> "SUGAR NO.11 Jul/10"), CREATED.DATE = structure(c(18395, 18395,
> 18396, 18396, 18392, 18392), class = "Date"), QUANITY = c(1,
> 1, -1, -1, 1, 1), CLOSING.PRICE = c("81.2000", "81.2000", "503.6000",
> "503.6000", "13.8900", "13.8900")), .Names = c("DESCRIPTION",
> "CREATED.DATE", "QUANITY", "CLOSING.PRICE"), row.names = c(NA,
> 6L), class = "data.frame")
>
> Where does the problem comes from?? Maybe from my sytem date ??
>
> TY for any help
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Senior in Psychology
University of California, Riverside
http://www.joshuawiley.com/



More information about the R-help mailing list