[R] Conversion from string to date type

Dirk Eddelbuettel edd at debian.org
Fri Mar 23 23:49:08 CET 2007


Hi Andreas,

Welcome to r-help :)

On 23 March 2007 at 22:21, Andreas Tille wrote:
| I'm on my very first steps to R and so I hope that I do
| not ask a really stupid questions but I did not found it
| via R-Search, in the FAQ or Google (BTW, the name "R" is
| not a really good seekiong criterion ;-) ).
| 
| I have a data file containing a table that containes
| dates and values like
| 
| date\t value1\t value2, ...
| 01.03.2007\t 17\t 42\t ...
| 02.03.2007\t 2\t 3\t ...
| 03.03.2007\t 47\t 11\t ...
| ...
| 
| I was perfectly able to read this file via
| 
| mydata <- read.csv(file='mydata.dat', sep = '\t', quote='', fill=TRUE,header=TRUE )

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

| but the date vector is a vector of strings instead of date values.
| I want to convert these strings into date values to be able
| to make graphs where date is the x-axis and value? the y-axis.

It could be worse :)  Often times, folks get confused when variables are
of type 'factor' (see the docs) instead of char. With character you are fine,
and you can do the converson, see help(strptime) or help(as.Date) which I am
using below --- note how you have to tell it the format in the standard C
notation. 

| 
| I would be really happy if someone could enlighten me how to
| do this conversion (and a hint how to do a graph as PNG) would
| be an extra bonus which would shorten my further reading of the
| docs).

Here you go:

> 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]
> mydata
        date value1 value2
1 2007-03-01     17     42
2 2007-03-02      2      3
3 2007-03-03     47     11
> with(mydata, plot(date, value1))                  ## [2]
> png("/tmp/mydata.png")                            ## [3]
> with(mydata, plot(date, value1))
> dev.off()                                         ## [4]
null device 
          1 
> 


[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

[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]) 

[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.

| Kind regards and thanks for your help

My pleasure. Happy R-ing,  Dirk

-- 
Hell, there are no rules here - we're trying to accomplish something. 
                                                  -- Thomas A. Edison



More information about the R-help mailing list