Hi, 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 ) 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. 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). Kind regards and thanks for your help Andreas. -- http://fam-tille.de
Here is how you can convert them to a Date object:> x <- c('01.03.2007','02.03.2007','03.03.2007') > y <- as.Date(x, format="%d.%m.%Y") > y[1] "2007-03-01" "2007-03-02" "2007-03-03"> str(y)Class 'Date' num [1:3] 13573 13574 13575>On 3/23/07, Andreas Tille <tillea@rki.de> wrote:> > Hi, > > 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 ) > > 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. > > 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). > > Kind regards and thanks for your help > > Andreas. > > -- > http://fam-tille.de > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
Read the help desk article in R-News 4/1 and see ?as.Date ?strptime (for setting the as.Date format= argument) Also, you might be interested in the zoo package library(zoo) ?read.zoo vignette("zoo") vignette("zoo-quickref") On 3/23/07, Andreas Tille <tillea at rki.de> wrote:> Hi, > > 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 ) > > 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. > > 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). > > Kind regards and thanks for your help > > Andreas. > > -- > http://fam-tille.de > > ______________________________________________ > R-help at stat.math.ethz.ch 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 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) > mydatadate 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] > mydatadate 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
Hello, Andreas I'm glad the previous replies helped you solve your problem. I think your original problem, though, was caused by a typo. Note the comma between the month and the year in your script and the period between the month and the year in Jim Holtman's script.>From Jim Holtman:> Here is how you can convert them to a Date object: > >> x <- c('01.03.2007','02.03.2007','03.03.2007') >> y <- as.Date(x, format="%d.%m.%Y") >> y>From Andreas Tillea: >Well, this is what I tried when reading the docs, but> mydata <- read.csv(file='mydata.dat', sep = '\t', > quote='', fill=TRUE,header=TRUE ) > datum <- as.Date(mydata["date"], "%d.%m,%y") >Error in as.Date.default(mydata["date"], "%d.%m,%y") :do not know how to convert 'mydata["date"]' to class "Date">I also tried:> datum <- strptime(imydata["date"], "%d.%m,%y") > datum >[1] NAHope this helps for the future Toby Toby Gass Department of Forest, Rangeland, and Watershed Stewardship Warner College of Natural Resources Colorado State University Fort Collins, CO 80523