Unexpected input error when doing subtraction in R - r

i try to run:
x = (1 / 1 – 2)
And here is the message:
Error: unexpected input in "x=(1 / 1 �"
I have the same issue with these sign : <- and * and minus, none of them are recognised.

You might be confronted to the so-called "zero-width space" which is not interpreted as a space. Check this wiki page:
If the blanks you have in your code example are this kind of character, it will make an error like this:
Error: unexpected input in "x=(1 / 1"
as some comments point out, one must be careful when pasting code from websites.

Related

Unexpected round behavior

When I type
round(1122700.625, 2)
I get 1122701
when I would expect 1122700.62 from my reading of ?round (nearest even)
Somewhat similarly for
round(205290.125, 2)
I get 205280.1
when I would expect 205290.12
round(205290.125,3) and round(205290.125,4) both give 205290.1
round(2.125, 2) gives 2.12 as expected
and
round(22222.125, 2) gives 22222.12 as expected
but round(222222.125, 2) gives 222222.1
and round(2222222.125, 2) gives 2222222
I have tried running R from the command line with --vanilla option with the same result (originally noted when using RStudio).
The help does also state "The realities of computer arithmetic can cause unexpected results" but
Am I missing something/what don't I understand?
Does this matter?!
I guess you looking for toFixed because the round only expect one param.
CLICK for ref.

Resolving "Error: subscript out of bounds" in a code loop

I am trying to run an R loop on an individual based model. This includes two lists referring to grid cells, which I originally ran into difficulties with because they returned the error: Error: (list) object cannot be coerced to type 'double'. I think I have resolved this error by typing "as.numeric(unlist(x))."
Example from code:
List 1:
dredg<-list(c(943,944,945,946,947,948,949...1744,1745))
dredging<-as.numeric(unlist(dredg)). I refer to 'dredging' in my code, not 'dredg.'
List 2:
nodredg<-list(c(612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,628,629,630,631))
dcells<-as.numeric(unlist(nodredg)) I refer to 'dcells' in my code, not 'nodredg.'
However, now when I use these two number arrays (if that's what they are) my code returns the error
Error in dcells[[dredging[whichD]]] : subscript out of bounds
I believe this error is referring to the following lines of code:
if(julday>=dstart[whichD] & julday<=dend[whichD] & dredging[whichD]!=0){
whichcells<-which(gridd$inds[gridd$julday==julday] %in% dcells[[ dredging[whichD] ]]) #identify cells where dredging is occurring
}
where:
whichD<-1
julday<-1+day
'dstart=c(1,25,75,100)dend=c(20,60,80,117)`
Here is the full block of code:
for (i in 1:time.steps){
qday <- qday + 1
for (p in 1:pop){
if (is.na(dolphin.q[p,1,i]!=dolphin.q[p,2,i])) {
dolphin.q[p,6,i]<-which.max(c(dolphin.q[p,1,i],dolphin.q[p,2,i]))
}else{
dolphin.q[p,6,i]<-rmulti(c(0.5,0.5))
}
usP<-usage[p,]
if(julday>=dstart[whichD] & julday<=dend[whichD] & dredging[whichD]!=0){
whichcells<-which(gridd$inds[gridd$julday==julday] %in% dcells[[ dredging[whichD] ]])
usP[whichcells]<-0
usP<-usP/sum(usP) #rescale the home range surface to 1
}
I was wondering if anyone could show me what is going wrong? I apologize if this is a very simple mistake I am making, I am a novice learner that has been scouring the internet, manuals, and Stack for days with no luck!
Thanks in advance!

Error in source() unexpected numeric constant

when I write an R script in a test.R file
nb <- 22
paste("Etudions le nombre: ",nb)
paste("Le logarithme népérien de ce nombre est: ", log(nb))
paste("La racine carrée de ce nombre est: ", sqrt(nb))
paste("Le cosinus de ce nombre est: ", cos(nb))
paste("Si on ajoute 3 au nombre ", nb, " on obtient: ", nb + 3)
q("ask")
I executed using :
source("/Users/shous/Desktop/Master2.0/LanguageR/test.R")
error message :
Error in source("/Users/shous/Desktop/Master2.0/LanguageR/test.R") :
/Users/shous/Desktop/Master2.0/LanguageR/test.R:1:9: unexpected numeric constant
1: nb <- 22
It can be encoding problem: unexpected numeric constant 1: nb <- 22
I guess you don't want to have this character Â.Try to change file encoding or rewrite problematic line (not copy paste).
unexpected numeric constant 1: nb <- 22
This is R telling you that it found the line
nb <- 22
and that isn't valid syntax. You can duplicate this simply on the command line with something like a = a 22, which also isn't valid syntax. You need to correct that line of code - I don't know what you want it to be, perhaps there is a missing line break, or perhaps it should be  + 22 or Â[22], etc...
The line that produces the error does not occur in the code you show, perhaps you should make sure you are running the right file.

Unexpected Closing Bracket in R?

eCHFao<- -0.141081
#eCHFa1<-0
eCHFb1<- 0.985833
eCHFg1<- 0.199665
meanCHF<- mean(XtCHF)
eVarCHF<- (array,3421)
eVarCHF[1]<- var(XtCHF)
abco<- c(meanCHF, XtCHF1)
for (i in 2:3421) {eVarCHF[i]<- exp(eCHFao + (eCHFb1*log(sqrt(eVarCHF[i-1]))}
this code can't run because of an unexpected closing bracket, I am not sure why the bracket is unexpected or what I am missing?
May be a language problem since the thing that is unexpected is a curly-brace and what is missing are two closing parens (aka "brackets" in some parts of the English-speaking world):
exp(eCHFao + (eCHFb1*log(sqrt(eVarCHF[i-1]))
1 2 3 4 43 # need more parens
The language terms referring to ('s, and {'s is a bit scattered around the world. You should have posted the full error message that started out:
Error: unexpected '}'
... so it didn't actually say "bracket".

Token at least one time

I need to use javaCC to identify the correct syntax from the errors.
< #horario : "<"<hora>":"<minuto>"-"<hora>":"<minuto>">"<retorno> >
< #horarioError : "<" ("()"|"(" (~["<", ">"])+")")? ">"<retorno> >
The first one is the correct syntax and the second one identify tge errors.
An error definition can contain the correct syntax but at least one time the error.
This is what I thought, but it classifies the correct ones as an error:
< error : (<diaSemana>(<horarioError>|<horario>)<retorno>)+<delimitador> >
How can I make to appear at least one time horarioError in any line?
Thanks.
First off I think you are trying to do way too much at the lexical level. I suggest using parsing (BNF) rules for all but the lowest level tokenizing.
To answer the question, you can do the following:
< error : ( <diaSemana>
<horario><retorno>
)*
( <diaSemana>
<horarioError><retorno>
)
( <diaSemana>
(<horarioError>|<horario>)<retorno>
)*
<delimitador>
>

Resources