Re: [R] Conversion from string to date type
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] NA Hope 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 __ 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.
[R] Conversion from string to date type
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.
Re: [R] Conversion from string to date type
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 [EMAIL PROTECTED] 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]] __ 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.
Re: [R] Conversion from string to date type
On Fri, 23 Mar 2007, jim holtman wrote: 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 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] NA I guess it is a very simple thing I'm doing wrong and missinterpret the docs. Thanks for the quick response 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.
Re: [R] Conversion from string to date type
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 [EMAIL PROTECTED] 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. __ 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.
Re: [R] Conversion from string to date type
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 __ 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.
Re: [R] Conversion from string to date type
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 __ 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.