[R] Troubles with the format of dates

Gabor Grothendieck ggrothendieck at gmail.com
Fri Nov 28 03:22:18 CET 2008


Perhaps just this:


Lines <- "DATUM   P1      P2
2006-11-16          425.21      423.99
2006-12-15     425.12   423.97
2007-01-16          425.16      424.06"

library(chron)
DF <- read.table(textConnection(Lines), header = TRUE, as.is = TRUE)
DF$DATUM <- chron(DF$DATUM, format = "y-m-d", out.format = "d.m.y")
plot(P1 ~ DATUM, DF)


# or using the zoo package this:

library(zoo)
z <- read.zoo(textConnection(Lines), header = TRUE,
	FUN = function(x) chron(x, format = "y-m-d", out.format = "d.m.y"))
plot(z)


On Thu, Nov 27, 2008 at 8:20 AM, Kathi <kathi at dropnet.ch> wrote:
> Dear useRs,
>
> I'm struggling again with date-related stuff: I am using R to draw water levels at certain measuring
> stations. My data comes as a tab-delimited text file and looks like this:
>
> DATUM   P1      P2
> ...
> 2006-11-16          425.21      423.99
> 2006-12-15     425.12   423.97
> 2007-01-16          425.16      424.06
> ...
>
> (measurements started in July 2004 and still continue on a monthly or bi-weekly basis)
>
> This is then plotted using this code:
>
> a<-read.table("dummy_data.tab", sep="\t", header=TRUE, na.strings=c(0))
> x<-as.Date(a$DATUM)
>
> plot(x, a$P1, axes=FALSE, xlim=c(as.Date("2004-07-01"), as.Date("2009-01-01")),
> ylim=c(423,428), col="red", pch=15, type="o", ylab="Kote [m a.s.l.]",
> main="Dummy Plot")
> points(x, a$P2, col="red", pch=17, type="o", lty="dotted")
> axis(2, at=423:428, tck=1, col="gray60")
> axis.Date(1, at=seq(as.Date("2004-07-01"), as.Date("2009-01-01"),
> by="month"), labels=seq(as.Date("2004-07-01"), as.Date("2009-01-01"),
> by="month"), tck=1, col="gray60")
>
> So far, I've been using this type of date format (yyyy-mm-dd) because that was the only way I could
> get  the whole thing to work when I started using R. However, living in Switzerland, I would prefer
> the dates to read 16.11.2006 i.e. dd.mm.yyyy or 16. Nov. 2006 (preferably using German names for
> the months, if possible).
>
> I've used the chron package to convert my dates to day mon year (16 Nov 2006, with or without
> spaces), i.e. I replaced the second line of code with:
>
> x<-format(chron(as.character(a$DATUM), format="y-m-d", out.format="day mon year"))
>
> but then I can't plot them any more, because as.Date() no longer is the correct function and R gives
> me an error saying there is an invalid xlim-value. Can someone please point me in the right
> direction?
>
> I could easily change the input data to be in dd.mm.yyyy format, if that helps.
>
> Thanks for your help,
>
> Kathi
>
>
> --
> DropNet AG - Das Unternehmen fuer Ihren Internet-Auftritt!
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list