jeudi 27 octobre 2016

While condition not working in R script but do in console

I have an incomprehensible problem in a R script. I got the following message after a while condition :

Error in while ((date_all[j]$year < (start_date$year + 10))) { : missing value where TRUE/FALSE needed

But the unbelievable thing is it's not working in my script, but if I copy paste exactly the same in the console window, it is working ! whithout any change, even the use of variable j in the condition (because it is previously defined so it has a value)

Here is my R script :

library(readxl)

data <- read_excel("C:/Users/alian/Desktop/EURIA/BE/Stoxx50_data.xlsm", sheet = 1,col_names = TRUE)
price_all = data$`Adj Close`    # adjusted closing value
N = length(price_all)

date_all=as.POSIXlt(data$Date[(1:N)])

j = 1
start_date=date_all[1]
date=.POSIXct(rep(NA,10))
price=rep(0,2520)

while ((date_all[j]$year < (start_date$year + 10) )) {
  date[j]=date_all[j]
  price[j]=price_all[j]
  j=j+1
}

It is working in the console and not in the script. And if I test the value of the condition in the console I do obtain TRUE or FALSE, depending on j value. And what I do in the while is neither the problem as I tested it, and nothing change if I replace it by a simple print to test also.

So, I don't see what's wrong, especially as it works in the console window.

So if you can help me, I would really appreciate ;)

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire