I have created what I believe to be a data frame. It is called env1$SPY. 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? Bob
Tena koe Robert Many times in R one can do things without a loop. In this case, see ?rle. You might also need to check substring or months depending on how you dates are stored. HTH .... Peter Alspach -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Robert Sherry Sent: Wednesday, 4 November 2015 1:27 p.m. To: r-help at r-project.org Subject: [R] Working with Data Frames I have created what I believe to be a data frame. It is called env1$SPY. 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? 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. The contents of this e-mail are confidential and may be ...{{dropped:14}}
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
Have you gone through any R tutorials? There are innumerable good ones on the web -- and one that ships with R (An Intro to R). Don't you think you should make an effort to learn some basics on your own before posting here? ... or do I misinterpret your question? (And if so, my apologies -- feel free to chastise me appropriately). Cheers, Bert Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Tue, Nov 3, 2015 at 4: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. > 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? > > 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.