# Interpreting “condition has length > 1” warning from `if` function

I have an array:

``````a <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
``````

and would like to implement the following function:

``````w<-function(a){
if (a>0){
a/sum(a)
}
else 1
}
``````

This function would like to check whether there is any value in `a` larger than 0 and if yes then divide each element by the sum of the total.

Otherwise it should just record 1.

I get the following warning message:

`````` Warning message:
In if (a > 0) { :
the condition has length > 1 and only the first element will be used
``````

How can I correct the function?

89

maybe you want `ifelse`:

``````a <- c(1,1,1,1,0,0,0,0,2,2)
ifelse(a>0,a/sum(a),1)

[1] 0.125 0.125 0.125 0.125 1.000 1.000 1.000 1.000
[9] 0.250 0.250
``````
Tuesday, June 1, 2021

35

``````\$filevar = "/images/staunton/{\$options_item['base_var']}.gif";
``````

you probably want:

``````\$filevar = \$_SERVER["DOCUMENT_ROOT"] . "/images/staunton/{\$options_item['base_var']}.gif";
``````

because you probably was confusing the relative path of your image inside the document root of your page vs the absolute path of the image in the filesystem.

Try it and comment back.

Wednesday, March 31, 2021

52

The documentation for `ifelse` states:

`ifelse` returns a value with the same shape as `test` which is filled with elements selected from either `yes` or `no` depending on whether the element of `test` is `TRUE` or `FALSE`.

Since you are passing test values of length 1, you are getting results of length 1. If you pass longer test vectors, you will get longer results:

``````> ifelse(c(TRUE, FALSE), c(1, 2), c(3, 4))
[1] 1 4
``````

So `ifelse` is intended for the specific purpose of testing a vector of booleans and returning a vector of the same length, filled with elements taken from the (vector) `yes` and `no` arguments.

It is a common confusion, because of the function's name, to use this when really you want just a normal `if () {} else {}` construction instead.

Tuesday, June 1, 2021

54

You get the error because `if` can only evaluate a `logical` vector of length 1.

Maybe you miss the difference between `&` (`|`) and `&&` (`||`). The shorter version works element-wise and the longer version uses only the first element of each vector, e.g.:

``````c(TRUE, TRUE) & c(TRUE, FALSE)
# [1] TRUE FALSE

# c(TRUE, TRUE) && c(TRUE, FALSE)
[1] TRUE
``````

You don't need the `if` statement at all:

``````mut1 <- trip\$Ref.y=='G' & trip\$Variant.y=='T'|trip\$Ref.y=='C' & trip\$Variant.y=='A'
trip[mut1, "mutType"] <- "G:C to T:A"
``````
Wednesday, June 2, 2021

50

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
``````
Wednesday, August 4, 2021