21

I wrote a code that uses 75(!!!) nested ifelse statements.

I know its probably the most inefficient code I could write, but when I tried to run it I received the following error:

>Error: unexpected ')' in:
"                                 ifelse(basic$SEMType=="ppc" &
 (grepl("Wellpoint Prospecting",basic$CategoryName)), "Wellpoint Prospecting","other"
                                     )))))))))))))))))))))))))))))))))))))"

I checked and doubled checked the number of ")". Its correct and the ifelse closes.

I also tried to run the nested ifelse by chunks, 15 at a time (and sometimes bigger chunks) and it works, so I figured the chance for syntax error is low.

Has anyone ever encountered such limitations?

I now run the code piece wise the inner ifelse first and record the result and move up the channel. This seems to work so far.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Yevgeny Tkach
  • 657
  • 4
  • 10
  • 3
    What are you trying to do with this many nested `ifelse` statements? – Dason Jul 31 '14 at 15:55
  • I need to cluster a large data frame into 75 groups with predefined set of rules, that combine conditions on three different columns. – Yevgeny Tkach Jul 31 '14 at 16:00
  • 6
    I don't know if there is a limit but you don't need `ifelse` statements. Just create a vector `group.id <- rep(NA, nrow(basic))`, and assign to it values like `group.id[basic$SEMType=="ppc" & grepl("Wellpoint Prospecting",basic$CategoryName)] <- "Wellpoint Prospecting"` and so on. Like this, each `ifelse` is substituted by an assignment, resulting in much clearer code (more than likely you can also write a function to simplify these assignments even more, but can't tell without looking at your code). – konvas Jul 31 '14 at 16:11
  • 1
    Note of warning: `ifelse` is extremely slow. Nesting them has the risk of exponentiating the slowness. See here: http://stackoverflow.com/a/16275201/1492421 – Ricardo Saporta Jul 31 '14 at 16:17
  • 3
    also, that error may be due to a missing closing quote occurring twice. – Ricardo Saporta Jul 31 '14 at 16:19
  • 2
    My code reading skills rapidly decline to 0 once I hit **2** nested `ifelse`'s. I really recommend posting (a small) example of what you're trying to achieve as a separate question, as you're not doing it right. – eddi Jul 31 '14 at 16:41
  • thanks a lot guys. I'll try later to alter the code and get rid of the ifelse nesting - I guess I must. I did manage running it by parts - so i doubt there was a syntax error though. thanks again – Yevgeny Tkach Jul 31 '14 at 16:56

1 Answers1

25

At least with this method, I seem to be able to create at most 50 levels of nesting

x<-"NA"
for(i in 1:50) {
    x<-paste0("ifelse(x==",i,",",i,",", x, ")")
}
x
eval(parse(text=x), list2env(list(x=21)))

But if i try 51, i get the error

Error in parse(text = x) : contextstack overflow at line 1

so maybe that is specific to parse. It seems odd that you would get a syntax error.

Thanks to the link provided by @shadow, Brian Ripley confirmed this in a 2008 response to an r-help question

In this particular case [contextstack overflow], it is saying that you have more than 50 nested parse contexts

And @Spacedman found where this limit is defined in the R source code

#define CONTEXTSTACK_SIZE 50
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    I get the same error if I manually copy and paste `x` for `1:51` – Señor O Jul 31 '14 at 16:05
  • 2
    Same error if put in a function def in a file and "sourced". Even if on multiple lines, and longer than 51 nestings I get the error on line 51. – Spacedman Jul 31 '14 at 16:08
  • 3
    According to Brian Ripley (https://stat.ethz.ch/pipermail/r-help/2008-March/157341.html) you can have at most 50 nested parse contexts. – shadow Jul 31 '14 at 16:09
  • 2
    Here it is in the code: https://github.com/wch/r-source/blob/c48b62e3b4dd43d939e21f4af87e877151f1840b/src/main/gram.y#L1158 – Spacedman Jul 31 '14 at 16:10