unexpected ')', which I can not figure out - r

I am getting the following error. I can not figure out what is missing, as I seem to have all my brackets matched up.
Error: unexpected ')' in:
"{
if (grepl(propertiesData[x,'city'],population[z,'NAME'],ignore.case=TRUE) & (propertiesData[x,'stateLong']==population[z,'STATENAME')"
Here is the code of the loop:
for (z in c(1:nrow(population)))
{
if (grepl(propertiesData[x,'city'],population[z,'NAME'],ignore.case=TRUE) & (propertiesData[x,'stateLong']==population[z,'STATENAME'))
{
propertiesData[x,'population']=population[z,'POP_2009']
break
}
}

==population[z,'STATENAME'))
Seems like you forgot the closing bracket. Add it in and see what happens:
==population[z,'STATENAME']))

You're missing one ] at the end of line.
...==population[z,'STATENAME'] ))

You are missing "]" at the end : (propertiesData[x,'stateLong']==population[z,'STATENAME']))

Related

R function furr::futures_map_dfr with unmatching opening bracket

I want to process something in parallel and use
result_future = furrr::future_map_dfr(1:length(firmi), function(i){ #Parallel version
...
}
Howeverm the error tooltip says "Unmatching opening bracket '(' and it refers to the bracket after furrr::future_map_dfr.
When I try to close it after function(i)), the error remains and it adds unexpected tocken ')' , referring to the new closing bracket.
result_future = furrr::future_map_dfr(1:length(firmi), function(i)){ #Parallel version
...
}
What is going on?
Thanks for any help!
M
The bracket has to be after the last {

R Error in if-nest and/or for-loop? Unexpected '}' in "}"

I'm trying to work with a table called 'data' in R.
My code is roughly like this:
for (i in nrow(data):1) {
data$meanApproval[i] = mean(data[max(1,i-11):i,5])
if(data$Poll = data[duplicated(data$Poll),]) {
data = data[!duplicated(data$Poll),]
}
}
It throws the error as in the title:
"Unexpected '}' in "}""
The syntax before I added this code gave no complaint, so I'm sure it's the section I've posted.
I know this may be considered a duplicate question, but I've studied all the other answers, and none of them help me with this issue. I can't find any missing bracket matches, and none of these are the result of a unicode misread.
Any help would be greatly appreciated.
The error message was misleading from the actual error in the code.
if(data$Poll = data[duplicated(data$Poll),]) {
...
}
should have read == for the if check
if(data$Poll == data[duplicated(data$Poll),]) {
...
}

using if/else with function in R

I have tried to write a simple code to compute the median but I got an error.
This is what I wrote
median<-function(x){odd.even<-length(x)%%2 if (odd.even = = 0)(sort(x)[length(x)/2]+sort(x)[1+length(x)/2])/2 else (sort(x)[ceiling(length(x)/2)])}
and this is the error I got
Error: unexpected 'if' in "median<-function(x){odd.even<-length(x)%%2 if"
Thanks
Try this (you forgot the brackets {)
median<-function(x){
odd.even<-length(x)%%2
if (odd.even == 0){
(sort(x)[length(x)/2]+sort(x)[1+length(x)/2])/2
} else {
(sort(x)[ceiling(length(x)/2)])
}
}
As pointed out if you want not to use bracket you can always do this, with a new line on the if statement :
median<-function(x){
odd.even<-length(x)%%2
if (odd.even == 0) (sort(x)[length(x)/2]+sort(x)[1+length(x)/2])/2 else (sort(x)[ceiling(length(x)/2)])
}
Also a return(x) at the end, might help the reading process, although it is not compulsory.

Lexical error at line 0, column 0

In the grammar below, I am trying configure any line that starts with ' as a single line comment and anything betweeen /' Multiline Comment '/. The single line comment works ok. But for some reason as soon as I press / or ' or ';' or < or '>' I get the error below. I don't have above characters configured. Shouldn't they be considered default and skip parsing ?
Error
Lexical error at line 0, column 0. Encountered: "\"" (34), after : ""
Lexical error at line 0, column 0. Encountered: ">" (62), after : ""
Lexical error at line 0, column 0. Encountered: "\n" (10), after : "-"
I have only included part of the code below for conciseness. For full Lexer definition please visit the link
TOKEN :
{
< WHITESPACE:
" "
| "\t"
| "\n"
| "\r"
| "\f">
}
/* COMMENTS */
MORE :
{
<"/'"> { input_stream.backup(1); } : IN_MULTI_LINE_COMMENT
}
<IN_MULTI_LINE_COMMENT>
TOKEN :
{
<MULTI_LINE_COMMENT: "'/" > : DEFAULT
}
<IN_MULTI_LINE_COMMENT>
MORE :
{
< ~[] >
}
TOKEN :
{
<SINGLE_LINE_COMMENT: "'" (~["\n", "\r"])* ("\n" | "\r" | "\r\n")?>
}
I can't reproduce every aspect of your problem. You say there is an error "as soon as" you enter certain characters. Here is what I get.
/ There is no error unless the next character is not a '. If the next character is not ', there is an error.
' I see no error. This is correctly treated as the start of comment
; There is always an error. No token can start with ;.
< There only an error if the next characters are not - or <-.
> There always is an error. No token can start with >
I'm not exactly sure why you would expect these not to be errors, since your lexer has no rules to cover these cases. Generally when there is no rule to match a prefix of the input and the input is not exhausted, there will be a TokenMgrError thrown.
If you want to eliminate all these TokenMgrErrors, make a catch-all rule (as explained in the FAQ):
TOKEN: { <UNEXPECTED_CHARACTER: ~[] > }
Make sure this is the very last rule in the .jj file. This rule says that, when no other rule applies, the next character is treated as an UNEXPECTED_CHARACTER token. Of course this just boots the problem up to the parsing level. If you really want the tokenizer to skip all characters that don't belong, just use the following rule as the very last rule:
SKIP : { < ~[] > }
For most languages, that would be an odd thing to do, which is why it is not the default.

R if-else not working [duplicate]

This question already has answers here:
if/else constructs inside and outside functions
(2 answers)
Closed 10 years ago.
What is wrong with this if-else in my R program?
if(is.na(result)[1])
print("NA")
else
coef(result)[[1]]
I'm getting an error:
> if(is.na(result)[1])
+ print("NA")
> else
Error: unexpected 'else' in "else"
> coef(result)[[1]]
So then I added curly braces around the if and the else and now I get this error:
> if(is.na(result)[1]) {
+     print("NA")
Error: unexpected input in:
"if(is.na(result)[1]) {
¬"
> } else {
Error: unexpected '}' in "}"
>     coef(result)[[1]]
Error: unexpected input in "¬"
> }
Error: unexpected '}' in "}"
It is that you are lacking curly braces. Try
if(is.na(result)[1]) {
print("NA")
} else {
coef(result)[[1]]
}
This matters less when your source an entire file at once but for line-by-line parsing (eg when you enter at the prompt) you have to tell R that more code is coming.
I think your problem is signaled with this error message:
"if(is.na(result)[1]) {
¬"
Notice that strange little symbol? You have gotten a non-printing character that looks like one of those old IBM end-of-line markers. It could be problem with your LOCALE strings or with your keyboard mapping or with code you got off the Internet. Hard to tell, but you should definitely try to get rid of them by backspacing over them/
Without curly brackets, the if ... else ... construct should be on one line only:
if(is.na(result)[1]) print("NA") else coef(result)[[1]]
If you only have one condition and two results it's syntactically easier to use ifelse()
ifelse(is.na(result)[1],
print("NA"),
coef(result)[[1]]
)
This runs for me without errors
x <- 1:5
result <- lm(c(1:3,7,6) ~ x)
if(is.na(result)[1]) {
print("NA")
} else {
coef(result)[[1]]
}
and produces
[1] -0.7

Resources