[R] difficulty in Formatting time series data
David Winsemius
dwinsemius at comcast.net
Sun Apr 22 20:34:13 CEST 2012
On Apr 22, 2012, at 2:18 PM, Raghuraman Ramachandran wrote:
> I also tried:
>
>> test$Date=as.POSIXct(test$Date,format="%m%d%y")
Well, as became apparent when you eventually offered an example, you
have dates in dd/mm/yyyy format, so it's hardly surprising that "it
didn't work" with a format that didn't match your data.
?strptime
?as.Date
>> test=cbind(test,day.of.week=format(test$Date,format="%A"))
>> head(test)
> Date Open High Low Close Volume Adj.Close day.of.week
> 1 <NA> 2.33 2.34 2.31 2.31 5366000 2.31 <NA>
> 2 <NA> 2.35 2.36 2.33 2.35 5382000 2.35 <NA>
> 3 <NA> 2.35 2.38 2.34 2.36 9606000 2.36 <NA>
> 4 <NA> 2.34 2.34 2.30 2.33 9596000 2.33 <NA>
> 5 <NA> 2.32 2.35 2.31 2.31 5941000 2.31 <NA>
> 6 <NA> 2.34 2.36 2.32 2.32 10332000 2.32
>
> It didnt help.
>
> Thx
> Raghu
>
> On Sun, Apr 22, 2012 at 6:41 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us
> >wrote:
>
>> On Sun, 22 Apr 2012, Hasan Diwan wrote:
>>
>> Raghu,
>>>
>>> On 22 April 2012 09:53, Raghuraman Ramachandran <optionsraghu at gmail.com
>>>> wrote:
>>>
>>> I have a data frame (from CSV file) which has its first column
>>> called
>>>> Date.
>>>> The Date is in the format mm/dd/yyyy. I was trying to get the
>>>> weekday for
>>>> these dates and I tried using wday() and day.of.week() functions
>>>> and both
>>>> of them gave me precisely the wrong answers. I think the issue
>>>> lies in
>>>> the
>>>> proper formatting of dates. The class of this column is a factor
>>>> class
>>>> and
>>>> hence I tried converting into POSIXlt, xts, zoo objects and yet I
>>>> could
>>>> not
>>>> get the weekday correctly. Anyone has any suggestions please?
>>>>
>>>>
>>> Try this:
>>> # assume dataIn is where the CSV files data is...
>>> dataIn$Date <- as.POSIXct(dataIn$Date, format='%m/%d/%y')
>>>
>>
>> By far the most common error I see is failing to import the Date
>> column as
>> character, instead allowing the import function to convert it to
>> factor,
>> after which computations (such as the above suggestion) use the
>> hidden
>> factor index instead of the visible character representation, which
>> further
>> mystifies beginners. The conversion above will only work correctly
>> if the
>> column was imported as character. E.g.
>>
>> dataIn <- read.csv( file="yourdatafile", as.is=TRUE )
>>
>> OP: Use the str() function to see what types you are working with,
>> and in
>> future R-help queries send dput() of the data and code you have
>> tried if we
>> are to be able to reproduce your attempts effectively rather than
>> reading
>> your mind.
>>
>>
>> dataIn <- cbind(dataIn, day.of.week = format(dataIn$Date,
>> format='%A')
>>>
>>
>> Why not just
>>
>> dataIn$day.of.week <- weekdays( dataIn$Date )
>>
>> ?
>>
>> ------------------------------**------------------------------**
>> ---------------
>> Jeff Newmiller The ..... ..... Go
>> Live...
>> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
>> Go...
>> Live: OO#.. Dead: OO#..
>> Playing
>> Research Engineer (Solar/Batteries O.O#. #.O#. with
>> /Software/Embedded Controllers) .OO#. .OO#.
>> rocks...1k
>>
>>
>> ______________________________**________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help
>> >
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html <http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list