[R] Ifelse statements and combining columns

Duncan Murdoch murdoch.duncan at gmail.com
Mon Jul 24 17:05:30 CEST 2017


On 24/07/2017 8:57 AM, Jeff Newmiller wrote:
> Not a reproducible example, so a bit of guessing here, but
>
> a) don't try to assign results to variables inside the ifelse. That is,  remove all the single-equals signs and "test" variables. If you really need to conditionally assign variables then use "if"... but chances are good you don't need that.

Those weren't assignments.  The arguments to ifelse() are named "test", 
"yes", and "no".

You're right though that the expressions were messed up.  Commas are 
missing between arguments, and the parentheses don't appear to be in the 
right place.

One suggestion for Kirsten:  instead of

dat$cond == "cond1" | dat$cond == "cond2"  |
dat$cond == "cond3" | dat$cond == "cond4"

it is shorter and a bit clearer to write

dat$cond %in% c("cond1", "cond2", "cond3", "cond4")

or maybe even

dat$cond %in% paste0("cond", 1:4)

Duncan Murdoch

> b) "closure" is effectively another word for"function"... functions like cbind are called with argument lists delimited by parentheses, not brackets, and having a missing argument to the cbind function will be of no use.
>



More information about the R-help mailing list