I have an xts object dollarReturn
. I am trying to sum all the values of the column CL where the previous day's return was below a certain threshold X.
I am getting the error:
Error in +.default (sum, value) : non-numeric argument to binary operator
My loops:
for(n in 1:nrow(dollarReturn)){
X = -2000
value = ifelse(dollarReturn$CL[n] < X,dollarReturn$CL[n+1],0)
sum = sum + value
print(n)
return(sum)
}
datasample:
structure(c(-2439.99999999999, -880.000000000002, 779.999999999992,
590.000000000018, -70.0000000000061, -189.999999999997), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", class = c("xts",
"zoo"), index = structure(c(1420416000, 1420502400, 1420588800,
1420675200, 1420761600, 1421020800), tzone = "UTC", tclass = "Date"), .Dim = c(6L,
1L), .Dimnames = list(NULL, "CL"))
2015-01-05 -2440
2015-01-06 -880
2015-01-07 780
2015-01-08 590
2015-01-09 -70
2015-01-12 -190