[R] if-else that returns vector

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Thu Oct 12 22:33:01 CEST 2023

What a strange question... ifelse returns a vector (all data in R is vectors... some have length 1, but length zero is also possible, as are longer vectors) that is exactly as long as the logical vector that you give it, filled with elements from the respective positions in the vectors supplied in the second and third arguments. Because your logical vector is length 1, you only get a vector with the first element of the second argument.

If you want to choose between one of two vectors considered wholly, then "if" is what you need:

result <- if (TRUE) c(1,2,3) else c(5,6)

On October 12, 2023 1:22:03 PM PDT, Christofer Bogaso <bogaso.christofer using gmail.com> wrote:
>Following expression returns only the first element
>ifelse(T, c(1,2,3), c(5,6))
>However I am looking for some one-liner expression like above which
>will return the entire vector.
>Is there any way to achieve this?
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

Sent from my phone. Please excuse my brevity.

More information about the R-help mailing list