Hi there, Using a quantmod function, I calculate the daily change between two points in a time series. However, I don't think I am using the data correctly. Code: getSymbols("^GSPC", src="yahoo") CloseData <- Cl(GSPC) Delta <- diff(CloseData, lag=1) for (i in 3:length(Delta)) { if (Delta[i]>Delta[i-1]) sum <- sum + Delta } I can't seem to use the Delta variable. Can anyone point me in the right direction to transform the variable into a usable one? Example:> Delta[i-1]GSPC.Close 2000-01-04 -55.8> Delta[i]GSPC.Close 2000-01-05 2.69> Delta[i-1]*10GSPC.Close 2000-01-04 -558> Delta[i-1]+Delta[i]Data: numeric(0) Index: NULL Thanks in advance, Eduard -- View this message in context: http://www.nabble.com/Help-with-data-type-tp24792149p24792149.html Sent from the R help mailing list archive at Nabble.com.
On Aug 3, 2009, at 10:48 AM, ehxpieterse wrote:> Using a quantmod function, I calculate the daily change between two > points > in a time series. However, I don't think I am using the data > correctly. > > Code: > getSymbols("^GSPC", src="yahoo") > CloseData <- Cl(GSPC) > Delta <- diff(CloseData, lag=1) > for (i in 3:length(Delta)) { > if (Delta[i]>Delta[i-1]) sum <- sum + Delta # ? Delta[i] ? > } > > I can't seem to use the Delta variable. Can anyone point me in the > right > direction to transform the variable into a usable one?Looking at the code I would have expected you to use only one element at a time from the Delta vector rather than trying to add a vector to a scalar. And did you ever initialize "sum"? And you should probably avoid using "sum" as a variable name anyway, since it is a function name.> > Example: >> Delta[i-1] > GSPC.Close > 2000-01-04 -55.8 >> Delta[i] > GSPC.Close > 2000-01-05 2.69 >> Delta[i-1]*10 > GSPC.Close > 2000-01-04 -558 >> Delta[i-1]+Delta[i] > Data: > numeric(0) > > Index: > NULL > > Thanks in advance,David Winsemius, MD Heritage Laboratories West Hartford, CT
It's because Delta isn't a number. Or, more accurately, Delta[1] and Delta[i-1] are not numbers. The are objects that have a class, and therefore an internal structure:> class(Delta[1])[1] "xts" "zoo" To understand what's going on, you need to understand the structure of these classes, and what operations you can and can't do on them. In the meantime, however, try as.numeric(Delta[i]) > as.numeric(Delta[i-1]) -Don At 7:48 AM -0700 8/3/09, ehxpieterse wrote:>Hi there, > >Using a quantmod function, I calculate the daily change between two points >in a time series. However, I don't think I am using the data correctly. > >Code: >getSymbols("^GSPC", src="yahoo") >CloseData <- Cl(GSPC) >Delta <- diff(CloseData, lag=1) >for (i in 3:length(Delta)) { > if (Delta[i]>Delta[i-1]) sum <- sum + Delta >} > >I can't seem to use the Delta variable. Can anyone point me in the right >direction to transform the variable into a usable one? > >Example: >> Delta[i-1] > GSPC.Close >2000-01-04 -55.8 >> Delta[i] > GSPC.Close >2000-01-05 2.69 >> Delta[i-1]*10 > GSPC.Close >2000-01-04 -558 >> Delta[i-1]+Delta[i] >Data: >numeric(0) > >Index: >NULL > >Thanks in advance, >Eduard > > >-- >View this message in context: >http://*www.*nabble.com/Help-with-data-type-tp24792149p24792149.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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062
Something like this should work: tmp <- as.Numeric(Delta) tmp3 <- tmp[ -c(1,2)] ## elements 3 through last (the "i" in the loop) tmp2 <- tmp[ -c(1,length(tmp)) ] ## elements 2 through next to last (the "i-1" in the loop) mysum <- sum( tmp3[tmp3 > tmp2]) ## sum of elements that are larger than the previous element -Don At 7:48 AM -0700 8/3/09, ehxpieterse wrote:>Hi there, > >Using a quantmod function, I calculate the daily change between two points >in a time series. However, I don't think I am using the data correctly. > >Code: >getSymbols("^GSPC", src="yahoo") >CloseData <- Cl(GSPC) >Delta <- diff(CloseData, lag=1) >for (i in 3:length(Delta)) { > if (Delta[i]>Delta[i-1]) sum <- sum + Delta >} > >I can't seem to use the Delta variable. Can anyone point me in the right >direction to transform the variable into a usable one? > >Example: >> Delta[i-1] > GSPC.Close >2000-01-04 -55.8 >> Delta[i] > GSPC.Close >2000-01-05 2.69 >> Delta[i-1]*10 > GSPC.Close >2000-01-04 -558 >> Delta[i-1]+Delta[i] >Data: >numeric(0) > >Index: >NULL > >Thanks in advance, >Eduard > > >-- >View this message in context: >http://*www.*nabble.com/Help-with-data-type-tp24792149p24792149.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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062