Is There a Limit for the Possible Number of Nested Ifelse Statements

Is there a limit for the possible number of nested ifelse statements

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

Exceeding limit with ifelse statements

You can put your patterns in a named vector, (notice the Other = "", this is a fall back when none of your pattern matches the string):

patterns <- c("At Home" = "home", "At School" = "school", "At Work" = "work", "Other" = "")

Then loop through the pattern and check if the string contains pattern:

match <- sapply(patterns, grepl, mydf$location, ignore.case = T)

Finally build up the new column buy checking the name of the matched pattern which is the one you want to replace with, if nothing matches, fall back to Other:

mydf$clean_loc <- colnames(match)[max.col(match, ties.method = "first")]
mydf

# A tibble: 10 x 3
# answer location clean_loc
# <int> <chr> <chr>
# 1 3 at home At Home
# 2 3 home At Home
# 3 3 in a home At Home
# 4 3 school At School
# 5 2 my school At School
# 6 4 School At School
# 7 5 Work At Work
# 8 1 work At Work
# 9 2 working At Work
#10 1 work usually At Work

ifelse grepl() more than 50 rows at once?

Build a vector with your strings (refer) and then test them with your data

refer <- setNames(c(1:10), c("k","2k","k1e","k2e","2k1e","k2","ke","k3e","1ek","ek"))
refer
k 2k k1e k2e 2k1e k2 ke k3e 1ek ek
1 2 3 4 5 6 7 8 9 10

# your data
da <- data.frame(data=c("2k","2k","k1e","k1e","2k1e","ke","ke","k3e","k3e","k1e","ed","dqw"))

cbind(da, ids=refer[da$data])
data ids
1 2k 2
2 2k 2
3 k1e 3
4 k1e 3
5 2k1e 5
6 ke 7
7 ke 7
8 k3e 8
9 k3e 8
10 k1e 3
11 ed NA
12 dqw NA

Maximum number of nested conditions allowed

No limit should exist for a REAL programming language. For VB.NET and Java I would be shocked if there is any limit. The limit would NOT be memory, because we are talking about COMPILE TIME constraints, not executing environment constraints.

This works just find in C#: It should be noted that the compiler might optimize this to not even use the IFs.

static void Main(string[] args)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{ Console.WriteLine("It works"); }
}
}
}
}
}
}
}
}
}
}
}
}
}
}

This should not be optimized too much:

static void Main(string[] args)
{
if (DateTime.Now.Month == 1)
{
if (DateTime.Now.Year == 2011)
{
if (DateTime.Now.Month == 1)
{
if (DateTime.Now.Year == 2011)
{
if (DateTime.Now.Month == 1)
{
if (DateTime.Now.Year == 2011)
{
if (DateTime.Now.Month == 1)
{
if (DateTime.Now.Year == 2011)
{
if (DateTime.Now.Month == 1)
{
if (DateTime.Now.Year == 2011)
{
if (DateTime.Now.Month == 1)
{
if (DateTime.Now.Year == 2011)
{
if (DateTime.Now.Month == 1)
{
if (DateTime.Now.Year == 2011)
{ Console.WriteLine("It works"); }
}
}
}
}
}
}
}
}
}
}
}
}
}
Console.ReadKey();
}

Circumventing the ifelse() limit to insert 156 unique values to a large-scale dataframe

You shouldn't need to nest the ifelse statements - just define the column ahead of time as NA and then fill it in repeatedly. For the no argument, you can supply the column itself and it'll fill in the values intelligently:

df <- head(data.frame(letters, LETTERS))

df$newcol <- NA
df$newcol <- ifelse(test = is.na(df$newcol) & df$letters=="a",
yes = "this is an a",
no = df$newcol)
df$newcol <- ifelse(test = is.na(df$newcol) & df$letters=="b",
yes = "this is a b",
no = df$newcol)
> df
letters LETTERS newcol
1 a A this is an a
2 b B this is a b
3 c C <NA>
4 d D <NA>
5 e E <NA>
6 f F <NA>

Is there such a thing as too many embedded if-statements?

Technically, I am not aware of any limitation to nesting.

It might be an indicator of poor design if you find yourself going very deep.

Some of what you posted looks like it may be better served as a case statement.

I would be concerned with readability, and code maintenance for the next person which really means it will be difficult - even for the first person (you) - to get it all right in the first place.

edit:

You may also consider having a class that is something like SearchableObject(). You could make a base class of this with common functionality, then inherit for ID, Name, etc, and this top level control block would be drastically simplified.

Javascript: Is there a limit to else if statements?

The problem is that if txtVal.length > 11, then either this is met:

    else if(txtVal.indexOf("%") != -1)

or this is met:

    else if(( txtVal.length  >0) &&  (txtVal.indexOf("%") == -1))

So that it will never reach the else if( txtVal.length > 11 ). You need to change this:

    else if(txtVal.indexOf("%") != -1) 
{
if((txtVal.indexOf('%')) != (txtVal.length-1))
{
alert(" % is only allowed at the end.");
return;
}
}

to this:

    else if(txtVal.indexOf("%") != -1 && txtVal.indexOf('%') != (txtVal.length-1))
{
alert(" % is only allowed at the end.");
return;
}

so that it doesn't "capture" the case where txtVal.indexOf('%') == (txtVal.length-1).



Related Topics



Leave a reply



Submit