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

  • SEARCH
  • Home
  • 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 478703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:43:06+00:00 2026-05-13T00:43:06+00:00

I recently discovered that you can conditionally assign a value with an if-else block.

  • 0

I recently discovered that you can conditionally assign a value with an if-else block.

y <- if(condition) 1 else 2

I realise that the use case for this is limited: if you have vectorised code, you would use the ifelse function instead. There is a performance benefit: if-else runs about 35x faster than ifelse in the scalar case on my machine (though you need to call it millions of times to notice much of a difference).

What is bugging me is that I can’t work out why this code works—I was amazed that it doesn’t just throw an error.

Another example suggests that if blocks behave like functions—they automatically return the last value in the block (though you can’t use a return statement in them).

y <- if(TRUE) 
{
   y <- 3
   4
}    # y is 4

Based on this, I guessed that maybe another environment was created when you entered an if block, but this doesn’t seem to be the case.

if(TRUE) sys.frames()    # NULL

Can anyone tell me what is happening under the hood, please?

  • 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-13T00:43:07+00:00Added an answer on May 13, 2026 at 12:43 am

    Let me focus on

    What is bugging me is that I can’t
    work out why this code works—I was
    amazed that it doesn’t just throw an
    error.

    Why do you think so? What happens is that we have

    1. y being assigned an expression
    2. that expression being an if if() ...
    3. leading to either a TRUE or FALSE in the test
    4. leading to either one of the two branches being entered
    5. leading to the respective code being evaluated
    6. leading to its final value being the value of the right-hand-side
    7. leading to this value being assigned to y

    Seems logical to me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently discovered that I can use lambdas to create simple event handlers. I
I recently discovered that you can set breakpoints in Xcode that will print to
I have recently discovered that I can make windows executables with g++ that have
I recently discovered that I could use the sp_help to get a table definition
We recently discovered that the Google Maps API does not play nicely with SSL.
I only just recently discovered that Visual C++ 2008 (and perhaps earlier versions as
Recently I have discovered that my release executable (made with msvc++ express 2008) becomes
Having recently discovered this method of development, I'm finding it a rather nice methodology.
I've discovered this idiom recently, and I am wondering if there is something I
I recently discovered the genshi.builder module. It reminds me of Divmod Nevow's Stan module.

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.