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 question.(5)

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

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Why do I get the condition has length > 1 and only the first element will be used error?
Next
Answered
Aaliyah Davis
  • 11
Aaliyah Davis
Asked: May 11, 20222022-05-11T02:19:33+00:00 2022-05-11T02:19:33+00:00In: r

Why do I get the condition has length > 1 and only the first element will be used error?

  • 11

. Advertisement .

..3..

. Advertisement .

..4..

Hi developer experts,

I have a small but frustrating use case, and so far, I couldn’t get my head around this problem & ideal solution. I am running my program, and I am facing one problem with the the condition has length > 1 and only the first element will be used.

Below is the command I used:

 a <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
w<-function(a){
  if (a>0){
     a/sum(a)
  }
  else 1
} 

when I run it, I get the following error:

  Warning message:
  In if (a > 0) { :
  the condition has length > 1 and only the first element will be used

I am looking forward to gaining some knowledge from all experts. Thank you, guys!

only the first element will be used
  • 3 3 Answers
  • 221 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

3 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    King
    2022-05-23T06:08:10+00:00Added an answer on May 23, 2022 at 6:08 am

    The error occured because you intended to pass f() a vector with length > 1.

    In R, if() and else() can only react to one logical element (true or false) at 1 time.

    The options I might suggest you are:

    1. use sapply(t, f)

    2. or use ifelse() because ifelse() can react to a logical vector with length > 1.

    Below is my example of using ifelse()

    f <- function(t) {
        /* ifelse(condition, do_if_true, do_if_false) */
        ifelse(t < 0, 0, (2*t)/((1+t^2)^2)
    }
    • 11
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Lucas Levasseur
    2022-05-25T18:30:13+00:00Added an answer on May 25, 2022 at 6:30 pm

    The if statement cannot be vectorized. ifelse is recommended for vectorized if statements. It is enough to write in your case

    w <- function(a){
    if (any(a>0)){
     a/sum(a)
    }
     else 1
    }

    Or a shorter vectorised version

    ifelse(a > 0, a/sum(a), 1)

    It all depends on which function you use. First function produces an output vector of length 1, while ifelse generates an output vector equal to a.

    • 14
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. elveralowe
    2022-07-12T05:40:29+00:00Added an answer on July 12, 2022 at 5:40 am

    Maybe you are interested in ifelse

    a <- c(1,1,1,1,0,0,0,0,2,2)
    ifelse(a>0,a/sum(a),1)
    
     [1] 0.125 0.125 0.125 0.125 1.000 1.000 1.000 1.000
     [9] 0.250 0.250
    • 1
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

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.