[R] Conversion from string to date type

Andreas Tille tillea at rki.de
Sat Mar 24 00:23:20 CET 2007


On Fri, 23 Mar 2007, Dirk Eddelbuettel wrote:

> Nit 1: read.csv() is for csv files which tend to have "," as a separator;
> read.table() is more useful here.

Well, that's correct.  read.csv was just a leftover from former
tests and finally it worked also this way - but thanks for the
hint anyway.

>> mydata <- read.table(file="/tmp/mydata.dat", sep="\t", header=TRUE)
>> mydata
>        date value1 value2
> 1 01.03.2007     17     42
> 2 02.03.2007      2      3
> 3 03.03.2007     47     11
>> mydata$date <- as.Date(mydata$date, "%d.%m.%Y")   ## [1]

Ahhh, the '$date' thingy did the trick!

> [1] As I mentioned, you need to supply a format unless your data lists as
> (for today) 2007-03-23 which is an ISO format

That's obvious.

> [2] The with() simply makes the indexing easier. Direct use is
> plot(mydata$date, mydata$value1) or also
> plot(mydata[,"date"], mydata[,"value1"]) or also
> plot(mydata[,1], mydata[,2])

Ahh, so many ways to do it right.  You must be really unlucky
if you manage to do it wrong as I did. ;-)

> [3] See the help for all the options on png, as well the numerous examples
> for plot to annotate, give titles, ...
>
> [4] dev.off() is critical to get the 'device' (here a file) closed.

Yea, read this in the docs.  Now I can start fine tuning and start
having fun with R. ;-)

> My pleasure. Happy R-ing,  Dirk

Thanks for the nice kick-start

           Andreas.

-- 
http://fam-tille.de



More information about the R-help mailing list