Hello, helpeRs, I have a vector of numbers from 1-365 (days of the year) that I would like to convert to a date. There are no NA's and no missing values. I did not insert leading zero's for numbers less than 100. Using the syntax: dat$doy.1 <- as.numeric(format(dat$doy, "%j" )) I get the following error message: Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L, : invalid 'trim' argument .What is the error message telling me? (Windows OS and R 2.11.1) Thank you. Toby
Toby, What is it that you're trying to accomplish? There seems to be several ideas confused in your post. It sounds like you have input x <- 1:365 and you want to call some function to return dates? Which date should be returned for input 1? January 1, 2010? Your error is because if you give format a numeric vector, the second argument is "trim", which needs to be TRUE or FALSE, and you're giving it "%j", which I can only assume to be an attempt to pass one of the ?strptime format specifiers to the function. But those come into play when you're trying to format a Date to a character string, which is not what you have at all. So please give a reproducible example (we don't have your 'dat' object, for instance) that we can run on our own to get a good answer to you. Toby Gass wrote:> Hello, helpeRs, > > I have a vector of numbers from 1-365 (days of the year) that I would > like to convert to a date. There are no NA's and no missing values. > I did not insert leading zero's for numbers less than 100. > > Using the syntax: > > dat$doy.1 <- as.numeric(format(dat$doy, "%j" )) > > I get the following error message: > > Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, > 3L, : > invalid 'trim' argument > > .What is the error message telling me? > (Windows OS and R 2.11.1) > > > Thank you. > > Toby > > ______________________________________________ > 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.
On Aug 27, 2010, at 3:29 PM, Toby Gass wrote:> Hello, helpeRs, > > I have a vector of numbers from 1-365 (days of the year) that I would > like to convert to a date. There are no NA's and no missing values. > I did not insert leading zero's for numbers less than 100. > > Using the syntax: > > dat$doy.1 <- as.numeric(format(dat$doy, "%j" ))That would not make a date. Try: dat$doy.1 <- as.Date("2010-01-01") + dat$doy -1> > I get the following error message: > > Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, > 3L, : > invalid 'trim' argument > > .What is the error message telling me? > (Windows OS and R 2.11.1)?format ... about its efforts to turn a string "%j" into a logical which is what a two argument call to format() without named arguments would be expecting in the second position. David Winsemius, MD West Hartford, CT
Toby - Since dat$doy is just a number, the default S3 method for format is used, where the second argument is the trim parameter. I suspect you are confusing format (which is for output) with strptime (which is for input). For example, strptime(dat$doy,'%j') will assume that the dates are in the current year, and return a POSIXlt object. Alternatively, you could pass an origin to as.Date: as.Date(dat$doy,origin='2009-12-31') to get a similar Date object. - Phil On Fri, 27 Aug 2010, Toby Gass wrote:> Hello, helpeRs, > > I have a vector of numbers from 1-365 (days of the year) that I would > like to convert to a date. There are no NA's and no missing values. > I did not insert leading zero's for numbers less than 100. > > Using the syntax: > > dat$doy.1 <- as.numeric(format(dat$doy, "%j" )) > > I get the following error message: > > Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, > 3L, : > invalid 'trim' argument > > .What is the error message telling me? > (Windows OS and R 2.11.1) > > > Thank you. > > Toby > > ______________________________________________ > 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. >