Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R if-else not working [duplicate]

Tags:

r

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 "}"
like image 493
CodeGuy Avatar asked Aug 03 '11 20:08

CodeGuy


1 Answers

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.

like image 73
Dirk Eddelbuettel Avatar answered Oct 09 '22 08:10

Dirk Eddelbuettel