0

I have the following code:

a=2 

if (length(a)>1){
b<<-rowSums(c[,-(1:2)][,c(T,F)]) }

else {b<<-sum(c[seq(3,length(x),2)]) }

I get the following error:

Error: unexpected 'else' in "    else"

I have no idea why I am getting this message. Any ideas?

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
user1723765
  • 6,179
  • 18
  • 57
  • 85
  • because its within a function – user1723765 Jan 18 '13 at 16:56
  • This part of the code is within a function and I want to write to a variable outside the function that's why I'm using <<, but if I change to = the error still remains (i am only pasting this part of the code) – user1723765 Jan 18 '13 at 16:59
  • 1
    I was going to edit the code formatting to clean it up, but then I looked at the error message and realized that the immediate error is actually coming from the formatting of the code itself. – joran Jan 18 '13 at 16:59
  • In particular, from `?Control`: "In particular, you should not have a newline between } and else to avoid a syntax error in entering a if ... else construct at the keyboard or via source." But given the use of the hated `<<-` I suspect there are other things going awry here as well. – joran Jan 18 '13 at 17:00
  • Jilber the things you propose are not related to the error message. These variables are all nicely defined in my program and do not give an error if I enter them separately – user1723765 Jan 18 '13 at 17:02
  • what is the problem with <<-? – user1723765 Jan 18 '13 at 17:03
  • 1
    Using `<<-` is considered (very) bad practice in general, and is often a sign that you need to rethink the way you've structured your code. Making assignments that stretch across environments like that can get very dangerous and cause problems that are nearly impossible to debug. People use it, but only very sparingly, and only when there is literally no other option. – joran Jan 18 '13 at 17:05

3 Answers3

1

I think you are looking for

ifelse(expr, if-case, else-case)

Actually it seems that your if is being executed and then you try to write an 'else' (but without if). Are you doing this code in a shell or is it written in a text file?

EDIT (explanation):

x <- 1
b <- ifelse(x == 1, 1,2)
cat(b,"\n")
[1] 1

So if x == 1, then b gets 1, otherwise it gets 2. You can exchange '1' and '2' with functions or anything you want.

1

In the R definition:

When the if statement is not in a block the else, if present, must appear on the same line as the end of statement2. Otherwise the new line at the end of statement2 completes the if and yields a syntactically complete statement that is evaluated. A simple solution is to use a compound statement wrapped in braces, putting the else on the same line as the closing brace that marks the end of the statement.

(R Language Definition)

So, the 'else' must me in the same line as '}'. Either

a=2 

if (length(a)>1){
b<<-rowSums(c[,-(1:2)][,c(T,F)]) } else {b<<-sum(c[seq(3,length(x),2)]) }

or

a=2 

if (length(a)>1){
b<<-rowSums(c[,-(1:2)][,c(T,F)]) 
} else {b<<-sum(c[seq(3,length(x),2)]) }

but is still a bad code/identation pattern. I'd try something like

a=2 

if (length(a)>1) {
    b<<-rowSums(c[,-(1:2)][,c(T,F)]) 
} else {
    b<<-sum(c[seq(3,length(x),2)]) 
}

to avoid further mistakes, or the suggested 'ifelse' function.

1

To solve your immediate problem, as @joran pointed out in the comments, you just need to have the } ending your if statement on the same line as the else.

a <- FALSE

if (a) {
    print("a is true")
} else {  ## Works
    print("a is false")
}

if (a) {
    print("a is true")
} ## Doesn't work
else {
    print("a is false")
}

When a line ends and a statement could have ended, R stops expecting more input related to that statement. This is the same reason that

1 +
2

gives 3, but

1
+ 2

won't.

I'd also strongly recommend that you rethink the <<-. It's much better form in R to have your functions return things than have them just change things. See also: Why is using `<<-` frowned upon and how can I avoid it?

Community
  • 1
  • 1
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294