|
I'm trying to convert a column in a data frame with dates from a
"Factor" type to a "Date Object" but I am encountering and error. (I am having trouble plotting an x,y scatter and I suspect it's something with my data format). I have a table with two columns and 8,000 rows. > dsort=read.delim("C:\\Documents and Settings\\E066582\\My Documents\\R\\R-2.13.0\\bin\\dsort.txt") "dsort" #name of data.frame > colnames(dsort)[1] #name of column 1 [1] "Date" > colnames(dsort)[2] #name of column 2 [1] "Qty" > class(dsort$Date) #checked data type of column "Date" and it came back as a factor [1] "factor" > Date2=as.Date(dsort$Date) #attempt at changing the data type from a factor to a date object (see error below). Error in charToDate(x) : character string is not in a standard unambiguous format Dates in my table are listed in "3/4/2007" format. StatBat2 [[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. |
|
Hi Nat,
I guess something like as.Date(as.character("3/4/2007"),format="%d/%m/%Y") should work - as.character() coerces the factors to characters, which the as.Date() function can work with, given the right format argument. HTH Stephan Am 01.06.2011 22:59, schrieb Struckmeier, Nathanael: > I'm trying to convert a column in a data frame with dates from a > "Factor" type to a "Date Object" but I am encountering and error. (I am > having trouble plotting an x,y scatter and I suspect it's something with > my data format). I have a table with two columns and 8,000 rows. > >> dsort=read.delim("C:\\Documents and Settings\\E066582\\My > Documents\\R\\R-2.13.0\\bin\\dsort.txt") > > > > "dsort" #name of > data.frame > >> colnames(dsort)[1] #name of column 1 > > [1] "Date" > >> colnames(dsort)[2] #name of column 2 > > [1] "Qty" > >> class(dsort$Date) #checked data type of column > "Date" and it came back as a factor > > [1] "factor" > >> Date2=as.Date(dsort$Date) #attempt at changing the data type from a > factor to a date object (see error below). > > Error in charToDate(x) : > > character string is not in a standard unambiguous format > > > > Dates in my table are listed in "3/4/2007" format. > > StatBat2 > > > > > [[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. |
|
In reply to this post by StatBat2
On Jun 1, 2011, at 3:59 PM, Struckmeier, Nathanael wrote:
> I'm trying to convert a column in a data frame with dates from a > "Factor" type to a "Date Object" but I am encountering and error. (I am > having trouble plotting an x,y scatter and I suspect it's something with > my data format). I have a table with two columns and 8,000 rows. > >> dsort=read.delim("C:\\Documents and Settings\\E066582\\My > Documents\\R\\R-2.13.0\\bin\\dsort.txt") > > > > "dsort" #name of > data.frame > >> colnames(dsort)[1] #name of column 1 > > [1] "Date" > >> colnames(dsort)[2] #name of column 2 > > [1] "Qty" > >> class(dsort$Date) #checked data type of column > "Date" and it came back as a factor > > [1] "factor" > >> Date2=as.Date(dsort$Date) #attempt at changing the data type from a > factor to a date object (see error below). > > Error in charToDate(x) : > > character string is not in a standard unambiguous format > > > > Dates in my table are listed in "3/4/2007" format. > > StatBat2 After seeing your subject line and your e-mail address, I was going to suggest a dozen roses and some Moose Munch... ;-) When you convert a character/factor to a Date, you need to specify the format of the object to be converted: > as.Date("3/4/2007", format = "%m/%d/%Y") [1] "2007-03-04" See ?as.Date and ?strptime for more information and format specifications. HTH, Marc Schwartz ______________________________________________ [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. |
|
In reply to this post by Stephan Kolassa
I'll give this a try and mess with what format to convert it to. Thanks
again! -----Original Message----- From: Stephan Kolassa [mailto:[hidden email]] Sent: Wednesday, June 01, 2011 2:10 PM To: Struckmeier, Nathanael Cc: [hidden email] Subject: Re: [R] Problems Dating.... Hi Nat, I guess something like as.Date(as.character("3/4/2007"),format="%d/%m/%Y") should work - as.character() coerces the factors to characters, which the as.Date() function can work with, given the right format argument. HTH Stephan Am 01.06.2011 22:59, schrieb Struckmeier, Nathanael: > I'm trying to convert a column in a data frame with dates from a > "Factor" type to a "Date Object" but I am encountering and error. (I am > having trouble plotting an x,y scatter and I suspect it's something with > my data format). I have a table with two columns and 8,000 rows. > >> dsort=read.delim("C:\\Documents and Settings\\E066582\\My > Documents\\R\\R-2.13.0\\bin\\dsort.txt") > > > > "dsort" #name of > data.frame > >> colnames(dsort)[1] #name of column 1 > > [1] "Date" > >> colnames(dsort)[2] #name of column 2 > > [1] "Qty" > >> class(dsort$Date) #checked data type of > "Date" and it came back as a factor > > [1] "factor" > >> Date2=as.Date(dsort$Date) #attempt at changing the data type from a > factor to a date object (see error below). > > Error in charToDate(x) : > > character string is not in a standard unambiguous format > > > > Dates in my table are listed in "3/4/2007" format. > > StatBat2 > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > 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. |
|
On Jun 1, 2011, at 5:14 PM, Struckmeier, Nathanael wrote: > I'll give this a try and mess with what format to convert it to. > Thanks > again! > You need to decide whether to use "%d/%m/%Y" or "%m/%d/%" , since the information you provided so far leaves that undetermined. -- David. > > > Hi Nat, > > I guess something like > as.Date(as.character("3/4/2007"),format="%d/%m/%Y") > should work - as.character() coerces the factors to characters, which > the as.Date() function can work with, given the right format argument. > > HTH > Stephan > > > Am 01.06.2011 22:59, schrieb Struckmeier, Nathanael: >> I'm trying to convert a column in a data frame with dates from a >> "Factor" type to a "Date Object" but I am encountering and error. (I > am >> having trouble plotting an x,y scatter and I suspect it's something > with >> my data format). I have a table with two columns and 8,000 rows. >> >>> dsort=read.delim("C:\\Documents and Settings\\E066582\\My >> Documents\\R\\R-2.13.0\\bin\\dsort.txt") >> >> >> >> "dsort" #name of >> data.frame >> >>> colnames(dsort)[1] #name of column 1 >> >> [1] "Date" >> >>> colnames(dsort)[2] #name of column 2 >> >> [1] "Qty" >> >>> class(dsort$Date) #checked data type of > column >> "Date" and it came back as a factor >> >> [1] "factor" >> >>> Date2=as.Date(dsort$Date) #attempt at changing the data type from > a >> factor to a date object (see error below). >> >> Error in charToDate(x) : >> >> character string is not in a standard unambiguous format >> >> >> >> Dates in my table are listed in "3/4/2007" format. >> >> StatBat2 >> >> >> >> >> [[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. David Winsemius, MD West Hartford, CT ______________________________________________ [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. |
| Powered by Nabble | Edit this page |
