Hi there, I have a spreadsheet in excel which consists of first column of dates and then subsequent columns that refer to prices of different securities on those dates. (the first row contains each series name) I saved the excel file as type csv and then imported to excel using prices=read.csv(file="C:/Documents and Settings/Hugh/My Documents/PhD/Option prices.csv",header = TRUE, sep = ",") This creates the correct time series data x<-ts(prices[,2]) but does not have the dates attached. However the dates refer to working days. So although in general they represent Monday-Friday this is not always the case because of holidays etc. How then can I create a time series where the dates are read in from the first column of the csv file? I can not find an example in R documentation where this is done? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Adding-dates-to-time-series-tp3524679p3524679.html Sent from the R help mailing list archive at Nabble.com.
Hi: I'd suggest using the zoo package; it allows you to use an index vector such as dates to map to the series. It is well documented and well maintained, with vignettes and an FAQ that can be found on its package help page (among other places). Here is a small example: dd <- data.frame(time = seq(as.Date('1993-01-01'), by = 'months', length = 200), s = rnorm(200)) head(dd, 3) time s 1 2003-01-01 1.4292491 2 2003-02-01 -1.0713998 3 2003-03-01 -0.4738791 library(zoo) ser <- with(dd, zoo(s, time)) # s is the series, time is the index vector str(ser) # ser is of class zoo plot(ser) # apply the plot method For finance applications, other possibilities include the xts and quantmod packages, both of which are built on zoo. HTH, Dennis On Sun, May 15, 2011 at 11:42 AM, Bazman76 <h_a_patience at hotmail.com> wrote:> Hi there, > > I have a spreadsheet in excel which consists of first column ?of dates and > then subsequent columns that refer to prices of different securities on > those dates. (the first row contains each series name) > > I saved the excel file as type csv and then imported to excel using > > prices=read.csv(file="C:/Documents and Settings/Hugh/My Documents/PhD/Option > prices.csv",header = TRUE, sep = ",") > > This creates the correct time series data > > x<-ts(prices[,2]) > > but does not have the dates attached. > > However the dates refer to working days. So although in general they > represent Monday-Friday this is not always the case because of holidays etc. > > How then can I create a time series where the dates are read in from the > first column of the csv file? I can not find an example in R documentation > where this is done? > > Thanks > > > -- > View this message in context: http://r.789695.n4.nabble.com/Adding-dates-to-time-series-tp3524679p3524679.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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. >
OK I got it to work thanks to your example plot(ser) however, ultiamtely a I need a stype ts object. So I used> xts <- as.ts(ser) > xtsTime Series: Start = 1 End = 732 Frequency = 1 which just gets me back to where I started with the correct raw data but no attached dates? It is possible to have a time series ts() object with irregular dates? -- View this message in context: http://r.789695.n4.nabble.com/Adding-dates-to-time-series-tp3524679p3525001.html Sent from the R help mailing list archive at Nabble.com.
On Sun, May 15, 2011 at 2:42 PM, Bazman76 <h_a_patience at hotmail.com> wrote:> Hi there, > > I have a spreadsheet in excel which consists of first column ?of dates and > then subsequent columns that refer to prices of different securities on > those dates. (the first row contains each series name) > > I saved the excel file as type csv and then imported to excel using > > prices=read.csv(file="C:/Documents and Settings/Hugh/My Documents/PhD/Option > prices.csv",header = TRUE, sep = ",") > > This creates the correct time series data > > x<-ts(prices[,2]) > > but does not have the dates attached. > > However the dates refer to working days. So although in general they > represent Monday-Friday this is not always the case because of holidays etc. > > How then can I create a time series where the dates are read in from the > first column of the csv file? I can not find an example in R documentation > where this is done? >Lines <- "time,s 2003-01-01,1.4292491 2003-02-01,-1.0713998 2003-03-01,-0.4738791" library(zoo) # F <- "C:/Documents and Settings/Hugh/My Documents/PhD/Option prices.csv" # z <- read.zoo(F, header = TRUE, sep = ",") # in reality we would read from the file as shown in the comments above # but here we do it this way so we can just copy it and paste it # verbatim into the R session z <- read.zoo(textConnection(Lines), header = TRUE, sep = ",") If you want xts then: library(xts) x <- as.xts(z) Note that ts is not good for dates so use zoo or xts. See ?read.zoo in the zoo package. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com