-3

I am loading time series date into R as a data frame. However, the time series dates aren't being displayed as string when I view the data, though the class of the date column is recognized as a date class. Without using zoo, how can I get the dataset to display the dates in string format. This is really for visual purposes to be able to see if the data is correct.

Date        Open    High    Low Close   Volume  Adj Close
3/8/2013    834.5   834.92  825.05  831.52  2911900 831.52
3/7/2013    834.06  836.62  829.58  832.6   2052700 832.6
3/6/2013    841.03  844 828.81  831.38  2873000 831.38
3/5/2013    828.93  840.15  828.9   838.6   4044100 838.6
3/4/2013    805.3   822.84  805 821.5   2775600 821.5

is displayed in R as:

Date    Open    High    Low Close   Volume  Adj.Close
15772   834.50  834.92  825.05  831.52  2911900 831.52
15771   834.06  836.62  829.58  832.60  2052700 832.60
15770   841.03  844.00  828.81  831.38  2873000 831.38
15769   828.93  840.15  828.90  838.60  4044100 838.60
15768   805.30  822.84  805.00  821.50  2775600 821.50
15765   797.80  807.14  796.15  806.19  2175400 806.19

I am using the command

data=read.csv("file location",header=T,colClasses=c("Date","numeric","numeric","numeric","numeric","numeric","numeric"))

Here is the dput output:

dput(head(data))
structure(list(Date = structure(c(15772, 15771, 15770, 15769, 
15768, 15765), class = "Date"), Open = c(834.5, 834.06, 841.03, 
828.93, 805.3, 797.8), High = c(834.92, 836.62, 844, 840.15, 
822.84, 807.14), Low = c(825.05, 829.58, 828.81, 828.9, 805, 
796.15), Close = c(831.52, 832.6, 831.38, 838.6, 821.5, 806.19
), Volume = c(2911900, 2052700, 2873000, 4044100, 2775600, 2175400
), Adj.Close = c(831.52, 832.6, 831.38, 838.6, 821.5, 806.19)), .Names = c("Date", 
"Open", "High", "Low", "Close", "Volume", "Adj.Close"), row.names = c(NA, 
6L), class = "data.frame")
jessica
  • 1,325
  • 2
  • 21
  • 35
  • can you give small sample data? – CHP Mar 11 '13 at 02:09
  • 1
    hi @jessica, welcome to SO. It would be helpful if you could please give us a `dput(.)` of your data. – Ricardo Saporta Mar 11 '13 at 02:16
  • 1
    Note that you were specifically asked to use a tool like `dput` (`str` would also help) do describe your data. The fundamental thing you have to understand about R is that the way that data is _printed to the screen_ may or may not have any connection to how it is actually being stored. There's a reason we ask you to do these things. Just pasting in the printed output is useless and helps no one. Odds are that your column was coerced to numeric, but there's no way to know for sure with the information you've provided. – joran Mar 11 '13 at 02:55
  • You're `dput` output displays the dates just fine for me. – joran Mar 11 '13 at 02:58
  • they display just fine for me too. question too localized? – CHP Mar 11 '13 at 09:13

2 Answers2

1

You will want to use as.Date and you will have to know the origin date for your data. For example, suppose you know that your date data counts from 1970-01-01, you could do this to convert:

dates <- c(1314, 1315, 1316)
as.Date(dates, format="%Y-%m-%d", origin="1970-01-01")

[1] "1973-08-07" "1973-08-08" "1973-08-09"

Update

Per Ricardo's comment, it appears your origin data is 1997-05-28

as.Date(dates, format="%Y-%m-%d", origin="1997-05-28")

[1] "2001-01-01" "2001-01-02" "2001-01-03"
Jason Morgan
  • 2,260
  • 21
  • 24
  • I'm not sure if this is the way to go about for the OP's issue. You are looking for `1314` to match to `2001-01-01`. – Ricardo Saporta Mar 11 '13 at 02:15
  • Thank you for you help Jason. So I am loading the data using the read.csv("file-location",header=T,colClasses=c("Date","numeric"...)). The date is the first column and is in the typical string format(e.g. "2001/01/01"). I don't know the origin to start the data from. I am bit surprised because R used to load this data and display the dates in the string format. However recently it's been showing the dates as numbers. Very quint. – jessica Mar 11 '13 at 02:19
  • Origin seems to be `"1997-05-28"`. quite a wierd origin. – RJ- Mar 11 '13 at 02:20
  • @RJ I agree it's a strange origin date. – Jason Morgan Mar 11 '13 at 02:23
  • 1
    "R used to ..." is indeed funny. Have you tried starting a clean session with `R --vanilla` (if you're on Windows see www.stat.ufl.edu/~winner/computing/r/running_r.doc ) to make sure that you don't have some odd objects lying around in your workspace/odd packages attached? This shouldn't happen in base R. – Ben Bolker Mar 11 '13 at 02:24
  • @jessica you will help yourself great deal if you add sample data to your question instead of just describing it. – CHP Mar 11 '13 at 02:24
  • Reedited the post with the data. Thought someone would know a quick simple function to solve the problem. – jessica Mar 11 '13 at 02:46
0

You could keep your date column as character and convert it afterwards to date.

Or as indicated in the help of read.table:

Otherwise there needs to be an as method (from package methods) for conversion from "character" to the specified formal class.

See this question how to apply this.

setClass("myDate")
setAs("character","myDate", function(from) as.Date(from, format="%d/%m/%Y"))

df <- read.table(header = TRUE, colClasses=c("myDate","numeric","numeric","numeric","numeric","numeric","numeric"),
text = 'Date  Open  High  Low Close VolumeAdj Close
3/8/2013    834.5   834.92  825.05  831.52  2911900 831.52
3/7/2013    834.06  836.62  829.58  832.6   2052700 832.6
3/6/2013    841.03  844 828.81  831.38  2873000 831.38
3/5/2013    828.93  840.15  828.9   838.6   4044100 838.6
3/4/2013    805.3   822.84  805 821.5   2775600 821.5')

str(df)
'data.frame':   5 obs. of  7 variables:
 $ Date     : Date, format: "2013-08-03" "2013-07-03" "2013-06-03" ...
 $ Open     : num  834 834 841 829 805
 $ High     : num  835 837 844 840 823
 $ Low      : num  825 830 829 829 805
 $ Close    : num  832 833 831 839 822
 $ VolumeAdj: num  2911900 2052700 2873000 4044100 2775600
 $ Close.1  : num  832 833 831 839 822
Community
  • 1
  • 1
EDi
  • 13,160
  • 2
  • 48
  • 57