Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6827591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:17:00+00:00 2026-05-26T22:17:00+00:00

(Background info: ifelse evaluates both of the expressions, even though only one will be

  • 0

(Background info: ifelse evaluates both of the expressions, even though only one will be returned. EDIT: This is an incorrect statement. See Tommy’s reply)

Is there any example where it makes sense to use ifelse in a non-vectorized situation? I think that “readability” could be a valid answer when we don’t care about small efficiency gains, but besides that, is it ever faster/equivalent/better-in-some-other-way to use ifelse when an if and then else would do the job?

Similarly, if I have a vectorized situation, is ifelse always the best tool to use? It seems strange that both expressions are evaluated. Is it ever faster to loop through one by one and do a normal if and then else? I’m guessing it would make sense only if evaluating the expressions took a really long time. Is there any other alternative that would not involve an explicit loop?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:17:01+00:00Added an answer on May 26, 2026 at 10:17 pm

    First, ifelse does NOT always evaluate both expressions – only if there are both TRUE and FALSE elements in the test vector.

    ifelse(TRUE, 'foo', stop('bar')) # "foo"
    

    And in my opinion:

    ifelse should not be used in a non-vectorized situation. It is always slower and more error prone to use ifelse over if / else:

    # This is fairly common if/else code
    if (length(letters) > 0) letters else LETTERS
    
    # But this "equivalent" code will yield a very different result - TRY IT!
    ifelse(length(letters) > 0, letters, LETTERS)
    

    In vectorized situations though, ifelse can be a good choice – but beware that the length and attributes of the result might not be what you expect (as above, and I consider ifelse broken in that respect).

    Here’s an example: tst is of length 5 and has a class. I’d expect the result to be of length 10 and have no class, but that isn’t what happens – it gets an incompatible class and length 5!

    # a logical vector of class 'mybool'
    tst <- structure(1:5 %%2 > 0, class='mybool')
    
    # produces a numeric vector of class 'mybool'!
    ifelse(tst, 101:110, 201:210)
    #[1] 101 202 103 204 105
    #attr(,"class")
    #[1] "mybool"
    

    Why would I expect the length to be 10? Because most functions in R “cycle” the shorter vector to match the longer:

    1:5 + 1:10 # returns a vector of length 10.
    

    …But ifelse only cycles the yes/no arguments to match the length of the tst argument.

    Why would I expect the class (and other attributes) to not be copied from the test object? Because < which returns a logical vector does not copy class and attributes from its (typically numeric) arguments. It doesn’t do that because it would typically be very wrong.

    1:5 < structure(1:10, class='mynum') # returns a logical vector without class
    

    Finally, can it be more efficient to “do it yourself”? Well, it seems that ifelse is not a primitive like if, and it needs some special code to handle NA. If you don’t have NAs, it can be faster to do it yourself.

    tst <- 1:1e7 %%2 == 0
    a <- rep(1, 1e7)
    b <- rep(2, 1e7)
    system.time( r1 <- ifelse(tst, a, b) )            # 2.58 sec
    
    # If we know that a and b are of the same length as tst, and that
    # tst doesn't have NAs, then we can do like this:
    system.time( { r2 <- b; r2[tst] <- a[tst]; r2 } ) # 0.46 secs
    
    identical(r1, r2) # TRUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First I will give a little background info so this question isn't completely without
Quick background info: of the three database tables that are relevant for this question,
Bit of Background info, I'm using An NSOutlineView with Core Data. This Is What
Usless Background Info Hello, all. This is my first post here, but I often
The following is some background info on this post. You can just skip to
Apologies in advance if this becomes a very long question... Background Info I have
Background Info: File Replication is Lame Currently, we have a massive, high-traffic ASP.NET web
Background info : I was handed a Tool, which was made using MS-Access 2007,
Some background info; LanguageResource is the base class LanguageTranslatorResource and LanguageEditorResource inherit from LanguageResource
Can someone give me some info/background info on how I might go about writing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.