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
> Where does the problem comes from?? Maybe from my sytem date ??Simply from not reading the options carefully enough, from ?strptime, ?%y? Year without century (00-99). If you use this on input, which century you get is system-specific. So don't! Most often values 00 to 68 are prefixed by 20 and 69 to 99 by 19 - that is the behaviour specified by the 2001 POSIX standard, but it does also say ?it is expected that in a future version of IEEE Std 1003.1-2001 the default century inferred from a 2-digit year will change?. ?%Y? Year with century. You want %Y, not %y.
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/
Change this line to :>pose$CREATED.DATE=as.Date(pose$CREATED.DATE,"%d/%m/%Y") # mind the capitalY>poseDESCRIPTION CREATED.DATE QUANITY CLOSING.PRICE 1 COTTON NO.2 Jul/10 2010-05-13 1 81.2000 2 COTTON NO.2 Jul/10 2010-05-13 1 81.2000 3 PALLADIUM Jun/10 2010-05-14 -1 503.6000 4 PALLADIUM Jun/10 2010-05-14 -1 503.6000 5 SUGAR NO.11 Jul/10 2010-05-10 1 13.8900 6 SUGAR NO.11 Jul/10 2010-05-10 1 13.8900 Cheers Joris On Tue, Jun 1, 2010 at 5:57 PM, arnaud Gaboury <arnaud.gaboury@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@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. >-- Joris Meys Statistical Consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control Coupure Links 653 B-9000 Gent tel : +32 9 264 59 87 Joris.Meys@Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]