I know this:
> library(date)
> x="1979-04-04"
> try=as.date(x, "ymd")
> print(try)
[1] 4Apr79
and that `x' here has to be a string, e.g.:
> x=1979-04-04
> print(x)
[1] 1971
I'm stuck in reading from a file. I say:
> A <- read.table(file="try")
> print(A)
V1 V2
1 1979-04-04 -1.04712042
2 1979-04-06 0.54538055
3 1979-04-09 0.09663392
4 1979-04-11 0.57119871
5 1979-04-12 0.73594112
6 1979-04-17 -1.54422087
7 1979-04-18 -0.20595691
8 1979-04-19 0.12700429
9 1979-04-20 0.42016807
10 1979-04-23 -1.46838241
I am confused - is V1 a number or a string? Looking at it, it must be
a string. But yet:
> library(date)
> try=as.date(A$V1, "ymd")
Error in as.date(A$V1, "ymd") : Cannot coerce to date format
In short, how do I parse in dates of the format yyyy-mm-dd (the ISO
8601 format) or the yyyymmdd format.
And if I may ask the next step: How do I tell R that I have a file
full of data all of which is time-series data, where V1 is the
datetime vector, and all the other columns are time-series, to do
things like ARMA models and ts plots with?
--
Ajay Shah Consultant
ajayshah at mayin.org Department of Economic Affairs
http://www.mayin.org/ajayshah Ministry of Finance, New Delhi
On Tue, 24 Feb 2004, Ajay Shah wrote:> I know this: > > > library(date) > > x="1979-04-04" > > try=as.date(x, "ymd") > > print(try) > [1] 4Apr79 > > and that `x' here has to be a string, e.g.: > > > x=1979-04-04 > > print(x) > [1] 1971 > > I'm stuck in reading from a file. I say: > > > A <- read.table(file="try") > > print(A) > V1 V2 > 1 1979-04-04 -1.04712042 > 2 1979-04-06 0.54538055 > 3 1979-04-09 0.09663392 > 4 1979-04-11 0.57119871 > 5 1979-04-12 0.73594112 > 6 1979-04-17 -1.54422087 > 7 1979-04-18 -0.20595691 > 8 1979-04-19 0.12700429 > 9 1979-04-20 0.42016807 > 10 1979-04-23 -1.46838241 > > I am confused - is V1 a number or a string? Looking at it, it must be > a string. But yet:It is a factor. See ?read.table.> > library(date) > > try=as.date(A$V1, "ymd") > Error in as.date(A$V1, "ymd") : Cannot coerce to date format > > In short, how do I parse in dates of the format yyyy-mm-dd (the ISO > 8601 format) or the yyyymmdd format. > > And if I may ask the next step: How do I tell R that I have a file > full of data all of which is time-series data, where V1 is the > datetime vector, and all the other columns are time-series, to do > things like ARMA models and ts plots with?You can't with an irregular series like this one. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Ajay Shah wrote:> I know this: > > > library(date) > > x="1979-04-04" > > try=as.date(x, "ymd") > > print(try) > [1] 4Apr79 > > and that `x' here has to be a string, e.g.: > > > x=1979-04-04 > > print(x) > [1] 1971 > > I'm stuck in reading from a file. I say: > > > A <- read.table(file="try") > > print(A) > V1 V2 > 1 1979-04-04 -1.04712042 > 2 1979-04-06 0.54538055 > 3 1979-04-09 0.09663392 > 4 1979-04-11 0.57119871 > 5 1979-04-12 0.73594112 > 6 1979-04-17 -1.54422087 > 7 1979-04-18 -0.20595691 > 8 1979-04-19 0.12700429 > 9 1979-04-20 0.42016807 > 10 1979-04-23 -1.46838241 > > I am confused - is V1 a number or a string? Looking at it, it must be > a string. But yet: > > > library(date) > > try=as.date(A$V1, "ymd") > Error in as.date(A$V1, "ymd") : Cannot coerce to date format > > In short, how do I parse in dates of the format yyyy-mm-dd (the ISO > 8601 format) or the yyyymmdd format. > > And if I may ask the next step: How do I tell R that I have a file > full of data all of which is time-series data, where V1 is the > datetime vector, and all the other columns are time-series, to do > things like ARMA models and ts plots with? >To see what class a column in a data.frame is the best way is to try: sapply(A, data.class) My guess is that "V1" is being read in as a factor (default). To convert to character to use with as.date, then use as.date(as.character(A$V1), "ymd") BTW, I would avoid using "try" as a variable name since "try" is a function in the base package. As for you second question, see the package ts for ARIMA modeling. -sundar
At 1:37 AM +0530 2/24/04, Ajay Shah wrote:>I know this: > > > library(date) > > x="1979-04-04" > > try=as.date(x, "ymd") > > print(try) > [1] 4Apr79 > >and that `x' here has to be a string, e.g.: > > > x=1979-04-04 > > print(x) > [1] 1971 > >I'm stuck in reading from a file. I say: > > > A <- read.table(file="try") > > print(A) > V1 V2 > 1 1979-04-04 -1.04712042 > 2 1979-04-06 0.54538055 > 3 1979-04-09 0.09663392 > 4 1979-04-11 0.57119871 > 5 1979-04-12 0.73594112 > 6 1979-04-17 -1.54422087 > 7 1979-04-18 -0.20595691 > 8 1979-04-19 0.12700429 > 9 1979-04-20 0.42016807 > 10 1979-04-23 -1.46838241 > >I am confused - is V1 a number or a string? Looking at it, it must be >a string. But yet:You can find out what it is by asking: mode(A$V1) class(A$V1) str(A$V1) and there are no doubt some other ways to ask.> > > library(date) > > try=as.date(A$V1, "ymd") > Error in as.date(A$V1, "ymd") : Cannot coerce to date format > >In short, how do I parse in dates of the format yyyy-mm-dd (the ISO >8601 format) or the yyyymmdd format. > >And if I may ask the next step: How do I tell R that I have a file >full of data all of which is time-series data, where V1 is the >datetime vector, and all the other columns are time-series, to do >things like ARMA models and ts plots with? > >-- >Ajay Shah Consultant >ajayshah at mayin.org Department of Economic Affairs >http://www.mayin.org/ajayshah Ministry of Finance, New Delhi > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA
There are a number of ways to do this. First read in the data
using the as.is=1 argument to ensure that the date is read
in as character:
z <- read.table(myfile, as.is=1, sep=" ",
col.names=c("Date", "Data"))
Now you can create a date column using date, POSIXct, chron or irts:
# date
require(date)
z$Date <- as.date(z$Date,format="ymd"))
# POSIXct
z$Date <- as.POSIXct(z$Date))
# chron
require(chron)
z$Date <- chron(z$Date, "y-m-d")
You can create a ts timeseries (which is regular) by ignoring t
he dates altogether:
z.ts <- ts(z$Data)
Or an irts irregular time series using package tseries:
require(tseries)
z.irts <- irts(as.POSIXct(z$Date), z$Data)
Or an its irregular time series using package its:
require(its)
z.its <- readcsvIts(myfile, sep=" ",
col.names=c("Date", "Data"))
Assuming z.ts was used you can do this:
ar(z.ts, order=1)
etc.
---
Date: Tue, 24 Feb 2004 01:37:23 +0530
From: Ajay Shah <ajayshah at mayin.org>
To: r-help <r-help at stat.math.ethz.ch>
Subject: [R] Need help on parsing dates
I know this:
> library(date)
> x="1979-04-04"
> try=as.date(x, "ymd")
> print(try)
[1] 4Apr79
and that `x' here has to be a string, e.g.:
> x=1979-04-04
> print(x)
[1] 1971
I'm stuck in reading from a file. I say:
> A <- read.table(file="try")
> print(A)
V1 V2
1 1979-04-04 -1.04712042
2 1979-04-06 0.54538055
3 1979-04-09 0.09663392
4 1979-04-11 0.57119871
5 1979-04-12 0.73594112
6 1979-04-17 -1.54422087
7 1979-04-18 -0.20595691
8 1979-04-19 0.12700429
9 1979-04-20 0.42016807
10 1979-04-23 -1.46838241
I am confused - is V1 a number or a string? Looking at it, it must be
a string. But yet:
> library(date)
> try=as.date(A$V1, "ymd")
Error in as.date(A$V1, "ymd") : Cannot coerce to date format
In short, how do I parse in dates of the format yyyy-mm-dd (the ISO
8601 format) or the yyyymmdd format.
And if I may ask the next step: How do I tell R that I have a file
full of data all of which is time-series data, where V1 is the
datetime vector, and all the other columns are time-series, to do
things like ARMA models and ts plots with?
?as.POSIXlt On Tue, 24 Feb 2004, Ajay Shah wrote:> I know this: > > > library(date) > > x="1979-04-04" > > try=as.date(x, "ymd") > > print(try) > [1] 4Apr79 > > and that `x' here has to be a string, e.g.: > > > x=1979-04-04 > > print(x) > [1] 1971 > > I'm stuck in reading from a file. I say: > > > A <- read.table(file="try") > > print(A) > V1 V2 > 1 1979-04-04 -1.04712042 > 2 1979-04-06 0.54538055 > 3 1979-04-09 0.09663392 > 4 1979-04-11 0.57119871 > 5 1979-04-12 0.73594112 > 6 1979-04-17 -1.54422087 > 7 1979-04-18 -0.20595691 > 8 1979-04-19 0.12700429 > 9 1979-04-20 0.42016807 > 10 1979-04-23 -1.46838241 > > I am confused - is V1 a number or a string? Looking at it, it must be > a string. But yet: > > > library(date) > > try=as.date(A$V1, "ymd") > Error in as.date(A$V1, "ymd") : Cannot coerce to date format > > In short, how do I parse in dates of the format yyyy-mm-dd (the ISO > 8601 format) or the yyyymmdd format. > > And if I may ask the next step: How do I tell R that I have a file > full of data all of which is time-series data, where V1 is the > datetime vector, and all the other columns are time-series, to do > things like ARMA models and ts plots with? > > -- > Ajay Shah Consultant > ajayshah at mayin.org Department of Economic Affairs > http://www.mayin.org/ajayshah Ministry of Finance, New Delhi > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
I just came across another package for irregular dates, zoo,
giving another alternative.
It can be used together with dates, chron or POSIXct. For
example, using chron:
require(zoo)
require(chron)
z.zoo <- zoo(z$Data, chron(z$Date, format="y-m-d"))
---
Date: Mon, 23 Feb 2004 17:22:42 -0500 (EST)
From: Gabor Grothendieck <ggrothendieck at myway.com>
To: <ajayshah at mayin.org>, <r-help at stat.math.ethz.ch>
Subject: RE: [R] Need help on parsing dates
There are a number of ways to do this. First read in the data
using the as.is=1 argument to ensure that the date is read
in as character:
z <- read.table(myfile, as.is=1, sep=" ",
col.names=c("Date", "Data"))
Now you can create a date column using date, POSIXct, chron or irts:
# date
require(date)
z$Date <- as.date(z$Date,format="ymd"))
# POSIXct
z$Date <- as.POSIXct(z$Date))
# chron
require(chron)
z$Date <- chron(z$Date, "y-m-d")
You can create a ts timeseries (which is regular) by ignoring t
he dates altogether:
z.ts <- ts(z$Data)
Or an irts irregular time series using package tseries:
require(tseries)
z.irts <- irts(as.POSIXct(z$Date), z$Data)
Or an its irregular time series using package its:
require(its)
z.its <- readcsvIts(myfile, sep=" ", col.names=c("Date",
"Data"))
Assuming z.ts was used you can do this:
ar(z.ts, order=1)
etc.
---
Date: Tue, 24 Feb 2004 01:37:23 +0530
From: Ajay Shah <ajayshah at mayin.org>
To: r-help <r-help at stat.math.ethz.ch>
Subject: [R] Need help on parsing dates
I know this:
> library(date)
> x="1979-04-04"
> try=as.date(x, "ymd")
> print(try)
[1] 4Apr79
and that `x' here has to be a string, e.g.:
> x=1979-04-04
> print(x)
[1] 1971
I'm stuck in reading from a file. I say:
> A <- read.table(file="try")
> print(A)
V1 V2
1 1979-04-04 -1.04712042
2 1979-04-06 0.54538055
3 1979-04-09 0.09663392
4 1979-04-11 0.57119871
5 1979-04-12 0.73594112
6 1979-04-17 -1.54422087
7 1979-04-18 -0.20595691
8 1979-04-19 0.12700429
9 1979-04-20 0.42016807
10 1979-04-23 -1.46838241
I am confused - is V1 a number or a string? Looking at it, it must be
a string. But yet:
> library(date)
> try=as.date(A$V1, "ymd")
Error in as.date(A$V1, "ymd") : Cannot coerce to date format
In short, how do I parse in dates of the format yyyy-mm-dd (the ISO
8601 format) or the yyyymmdd format.
And if I may ask the next step: How do I tell R that I have a file
full of data all of which is time-series data, where V1 is the
datetime vector, and all the other columns are time-series, to do
things like ARMA models and ts plots with?