[R] inefficient ifelse() ?

R. Michael Weylandt michael.weylandt at gmail.com
Wed Mar 28 17:30:35 CEST 2012


Just use a regular if statement:

X <- if(b == "YY") a else substr(a, 1,3)

Michael

On Wed, Mar 28, 2012 at 11:19 AM, Manta <mantino84 at libero.it> wrote:
> I have a similar problem. I have a dataset and an element. If the element is
> equal to "YY", I want to take the first column of the dataset, otherwise I
> want to take the second column. The following does not work, as it only
> evaluates the first element. Any idea?
>
> a=c("AAAXXX","BBBXXX")
> a=merge(a,c("AAA","BBB"))
> b="YY"
>> ifelse(b=="YY",a,substr(a,1,3))
> [1] "AAAXXX"
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/inefficient-ifelse-tp3330423p4512579.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.



More information about the R-help mailing list