[R] Working with Data Frames

Joshua Ulrich josh.m.ulrich at gmail.com
Wed Nov 4 01:56:59 CET 2015


On Tue, Nov 3, 2015 at 6:26 PM, Robert Sherry <rsherry8 at comcast.net> wrote:
> I have created what I believe to be a data frame. It is called env1$SPY.

It's not a data.frame.  You can use str() to look at the *str*ucture
of an object:

R> str(env1$SPY)
An ‘xts’ object on 1995-01-03/2015-11-02 containing:
  Data: num [1:5247, 1:6] 45.7 46 46 46.1 46 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:6] "SPY.Open" "SPY.High" "SPY.Low" "SPY.Close" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:
List of 2
 $ src    : chr "yahoo"
 $ updated: POSIXct[1:1], format: "2015-11-03 18:51:48"

> The r statement head( env1$SPY ) produces the following output:
>
>            SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
> 1995-01-03  45.7031  45.8437 45.6875   45.7812     324300 31.55312
> 1995-01-04  45.9843  46.0000 45.7500   46.0000     351800 31.70392
> 1995-01-05  46.0312  46.1093 45.9531   46.0000      89800 31.70392
> 1995-01-06  46.0937  46.2500 45.9062   46.0468     448400 31.73617
> 1995-01-09  46.0312  46.0937 46.0000   46.0937      36800 31.76850
> 1995-01-10  46.2031  46.3906 46.1406   46.1406     229800 31.80082
>
> The above data from was created by the following commands:
>     library( quantmod )
>     env1 <- new.env()
>     getSymbols("SPY", src = 'yahoo', from = '1995-01-01', env = env1,
> auto.assign = T)
>
> Now, what I want to do is to loo through the data look for when the month
> changes. What is the proper way of writing a for loop in
> R and access the date field?
>
Since the above is an xts object, there is no "date field".  There's
an index attribute.  It would probably help you a lot to read the xts
and zoo vignettes/FAQ.

You would also get better help if you were more specific about what
you're trying to do.  There's probably an easier way to do what you
intend to do with a loop.

> Bob
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com



More information about the R-help mailing list