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 7037461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:32:53+00:00 2026-05-28T01:32:53+00:00

As the question asks, is there a control sequence in R similar to C’s

  • 0

As the question asks, is there a control sequence in R similar to C’s ternary operator? If so, how do you use it? 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-28T01:32:53+00:00Added an answer on May 28, 2026 at 1:32 am

    As if is function in R and returns the latest evaluation, if-else is equivalent to ?:.

    > a <- 1
    > x <- if(a==1) 1 else 2
    > x
    [1] 1
    > x <- if(a==2) 1 else 2
    > x
    [1] 2
    

    The power of R is vectorization. The vectorization of the ternary operator is ifelse:

    > a <- c(1, 2, 1)
    > x <- ifelse(a==1, 1, 2)
    > x
    [1] 1 2 1
    > x <- ifelse(a==2, 1, 2)
    > x
    [1] 2 1 2
    

    Just kidding, you can define c-style ?::

    `?` <- function(x, y)
        eval(
          sapply(
            strsplit(
              deparse(substitute(y)), 
              ":"
          ), 
          function(e) parse(text = e)
        )[[2 - as.logical(x)]])
    

    here, you don’t need to take care about brackets:

    > 1 ? 2*3 : 4
    [1] 6
    > 0 ? 2*3 : 4
    [1] 4
    > TRUE ? x*2 : 0
    [1] 2
    > FALSE ? x*2 : 0
    [1] 0
    

    but you need brackets for assignment 🙁

    > y <- 1 ? 2*3 : 4
    [1] 6
    > y
    [1] 1
    > y <- (1 ? 2*3 : 4)
    > y
    [1] 6
    

    Finally, you can do very similar way with c:

    `?` <- function(x, y) {
      xs <- as.list(substitute(x))
      if (xs[[1]] == as.name("<-")) x <- eval(xs[[3]])
      r <- eval(sapply(strsplit(deparse(substitute(y)), ":"), function(e) parse(text = e))[[2 - as.logical(x)]])
      if (xs[[1]] == as.name("<-")) {
        xs[[3]] <- r
            eval.parent(as.call(xs))
      } else {
        r
      }
    }       
    

    You can get rid of brackets:

    > y <- 1 ? 2*3 : 4
    > y
    [1] 6
    > y <- 0 ? 2*3 : 4
    > y
    [1] 4
    > 1 ? 2*3 : 4
    [1] 6
    > 0 ? 2*3 : 4
    [1] 4
    

    These are not for daily use, but maybe good for learning some internals of R language.

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

Sidebar

Related Questions

Subject to this question, asks it all:How effective is executeBatch method? Is there a
as the question asks, is there a way of finding when a page was
There's an old question that asks this very thing, but a lot has changed
This question asks how to restrict for a whole server. I just want to
This question asks how to compute the Cartesian product of a given number of
This question asks about file name the servers sends and the fix was quite
Pretty much as the question asks. Answers preferably in pseudo code and referenced. The
This other SO question asks about an autocomplete textbox in WPF. Several people have
In this question superwiren asks about the pitfalls for converting a .net 2.0 solution
This post asks this question but doesn't really give an answer, so I thought

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.