[R] if-else that returns vector

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Oct 12 22:35:01 CEST 2023


Às 21:22 de 12/10/2023, Christofer Bogaso escreveu:
> Hi,
> 
> 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
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hello,

I don't like it but


ifelse(rep(T, length(c(1,2,3))), c(1,2,3), c(5,6))


maybe you should use


max(length(c(1, 2, 3)), length(5, 6)))


instead, but it's still ugly.

Hope this helps,

Rui Barradas


-- 
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença de vírus.
www.avg.com



More information about the R-help mailing list