Array where there is a non-numeric argument to binary operator - r

I was running my code, when I received the following error message:
"Error in t-1: non-numeric argument to binary operator"
The error is in p[t]<-p[t-1] in the function:
p<-rep(0, dimension)
price<-function(n, dimension){
p[t]<-p[t-1]
...
}
this function is recalled into another function where p[t=0] is equal to 1. T represents the time at which an order is submitted. At the beginning, i.e. for t=0, p[t]<-1. I also tried to change p[t-1] into p[t] and p[t] into p[t+1], but it gives me the same error.
Could you please tell me how I can fix this error?
Thank you

Related

"argument 'pattern' is missing" - showing up after every command after calling readRDS()

I encountered this weird issue in R, when after I call readRDS(<file_path>) it reads the file in correctly, but throws an argument "pattern" is missing, with no default error. I would not mind if it was just one time, but it throws this error every time I call any other function after readRDS(), for example:
> fit <- readRDS(<file_path>)
argument "pattern" is missing, with no default
> head(fit)
<SHOWS THE CORRECT OUTPUT!>
argument "pattern" is missing, with no default
> head(<another_file_read_by_fread()>)
<SHOWS THE CORRECT OUTPUT!>
argument "pattern" is missing, with no default
It is frustrating and makes no sense to me, does anyone know what is going on?

How do i resolve this error " subscript out of bounds"?

I would like to resolve the error I am getting while running the code below.
I have tried to debug the code but I am still getting the same error message.
glmer_results= evaluatr.univariate(analysis)
lapply(glmer_results,evaluatr.univariate.plot)
Error in variance.vars[[i]] : subscript out of bounds
Calls: ... eval -> eval -> evaluatr.univariate -> evaluatr.impact.pre
Execution halted
It is very hard to provide an answer without a reproducible example. However, the subscript out of bounds error message means that you are trying to subset an element, in this case variance.vars appears to be a list, but the element you are trying to fetch doesn't exist.
For example, if variance.vars is of length 2, but your index i goes up to three, then variance.vars[[1]] and variance.vars[[2]] will work and return the corresponding list element, but variance.vars[[3]] will fail and give the error message subscript out of bounds since the third list element doesn't exist. This is true if you try to extract elements from vectors or matrices as well.

R Programming - if condition execution

I am executing following code in R using "IF" and the condition executed (I was expecting, it will give error message),
if("TRUE") print("ok")
can some one help me in understanding the logic behind the code execution?
My understanding is that "if statement" will execute when the conditional expression is true.
In the above code, I have given character as input, but the if condition is executed, which surprise me.
R converts the argument of if statement if it is interpretable as logical. In this case "TRUE" is interpretable as logical. Please see that as.logical("TRUE") returns TRUE. However, if("HELLO") print("ok") would not work and you will get the error:
Error in if ("HELLO") print("ok") :
argument is not interpretable as logical
You just need to fix the error in your syntax. Try this:
if (TRUE){
print("ok")
}

Error in Init statement, not sure of passing vector in viterbi

Error in E[, ] = emissionProbs[, ] :
number of items to replace is not a multiple of replacement length
Calls: initHMM
This is the warning coming.
library(HMM)
transitions <- read.table("/home/subjects/comp90016/assignments/assignment3/transitions.txt")
emissions <- read.table("/home/subjects/comp90016/assignments/assignment3/emissions.txt")
hmm=initHMM(States=transitions[,1],Symbols=c("CP0","CP1","CP2","CP3","CP4"),transProbs=as.matrix(transitions),emissionProbs=as.matrix(emissions))
results<-viterbi(hmm,(my_vector[])[1]))
print (results)
There is vector called my_vector.
I am trying the following thing in Viterbi to derive a specific result but execution is getting halted due to the warning. Might be some other error while passing my_vector also.
Thanks for helping.
hmm=initHMM(States=transitions[,1],Symbols=c("0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20),transProbs=as.matrix(transitions),emissionProbs=as.matrix(emissions))
This was the error. Thanks

How to mix flog.fatal and stop

I am starting to use the futile.logger package. There is a nice FATAL error logging that I would like to use in conjuction with stop.
But say I do :
stop(flog.fatal('crash for some reason'))
I get
[FATAL] [2015-07-06 22:46:54] [base.stop] crash for some reason
Error:
[FATAL] [2015-07-06 22:46:54] [base.stop] crash for some reason
How can I just stop (abort the program), after logging what the flog.fatal logged
So [FATAL] [2015-07-06 22:46:54] [base.stop] crash for some reason and that's it
Your question as stated seems trivial? Just do
flog.fatal('crash for some reason'); stop()
?
(I personally find futile.logger more useful for writing text messages to a file, not to the console, but also print the messages to the console with R's conditions message(), warning(), stop() etc.
The value returned by flog.fatal is a character vector (length 1) which stop prints.
res <- flog.fatal('crash for some reason')
print(res)
As alternative you can use ftry function from the futile.logger package.
> futile.logger::ftry(log("a"))
ERROR [2017-08-22 10:50:51] non-numeric argument to mathematical function
Error in log("a") : non-numeric argument to mathematical function

Resources