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/How to avoid the error in x[floor(d)] + x[ceiling(d)] : non-numeric argument to binary operator?
Next
Answered
Tom
  • 2
Tom
Asked: July 2, 20222022-07-02T15:15:59+00:00 2022-07-02T15:15:59+00:00In: Error

How to avoid the error in x[floor(d)] + x[ceiling(d)] : non-numeric argument to binary operator?

  • 2

. Advertisement .

..3..

. Advertisement .

..4..

 The error in x[floor(d)] + x[ceiling(d)] : non-numeric argument to binary operator appears when I run my program:

out.liar <- function(variable) { # arguments can be called whatever
remove <- boxplot(variable, plot = F)$out
variable[variable %in% remove] <- NA
return(variable)
}

When I pass a character vector to the out.liar() function, it throws an error.

out.liar(letters)
## Error in x[floor(d)] + x[ceiling(d)]: non-numeric argument to binary operator

Please tell me the cause and how to fix the above error.

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

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-07-02T15:40:58+00:00Added an answer on July 2, 2022 at 3:40 pm

    The cause:  The error in x[floor(d)] + x[ceiling(d)] : non-numeric argument to binary operator happened was that the letter vector was sent from out.liar() to boxplot(), which then passed it on to the + function. The code failed because you cannot add letters, which broke boxplot() and out.liar().

    Solution: You could also want to add the ability to locate outliers in a data frame to the function. To accomplish that, you must first make the function recognize which columns in the provided data frame are numerical before executing the code on each of these columns. The function should then return the full data frame, with the numeric columns updated and the non-numeric columns.

    out.liar <- function(variable) { # arguments can be called whatever
    if (class(variable) == "data.frame") {
    # identify numeric variables by applying the is.numeric
    # function to each column of the data frame
    num_vars <- unlist(lapply(variable, is.numeric))
    # run the out.liar function on each numeric column
    # this kind of recurrence where you run a function inside of itself
    # is allowed and often used!
    variable[num_vars] <- lapply(variable[num_vars], out.liar)
    } else {
    remove <- boxplot(variable, plot = F)$out
    variable[variable %in% remove] <- NA
    }
    return(variable)
    }

    Check to see whether it functions, make a data frame first:

    df <- data.frame(id = factor(1:10), var1 = c(100, rnorm(9)),
    var2 = c(rchisq(9, 4), 100), var3 = LETTERS[1:10])
    df
    ## id var1 var2 var3
    ## 1 1 100.00000000 3.9174945 A
    ## 2 2 0.47461824 2.5883314 B
    ## 3 3 -1.76634605 1.6285662 C
    ## 4 4 -0.04140268 3.5953417 D
    ## 5 5 -0.74529598 8.3492440 E
    ## 6 6 0.55631124 0.2957526 F
    ## 7 7 0.57117284 2.8894209 G
    ## 8 8 2.92466663 12.4805552 H
    ## 9 9 0.45180224 2.5062931 I
    ## 10 10 -1.31348905 100.0000000 J

    Followed by feeding df to out.liar():

    out.liar(df)
    ## id var1 var2 var3
    ## 1 1 NA 3.9174945 A
    ## 2 2 0.47461824 2.5883314 B
    ## 3 3 -1.76634605 1.6285662 C
    ## 4 4 -0.04140268 3.5953417 D
    ## 5 5 -0.74529598 8.3492440 E
    ## 6 6 0.55631124 0.2957526 F
    ## 7 7 0.57117284 2.8894209 G
    ## 8 8 NA 12.4805552 H
    ## 9 9 0.45180224 2.5062931 I
    ## 10 10 -1.31348905 NA J

    Finally, verify that the adjustment does not affect the function’s ability to work on single columns:

    x <- rnorm(20, 9, 2)
    x[2] <- 1520
    out.liar(x)
    ## [1] 11.227530 NA 11.122428 9.046352 9.047519 9.011521 5.715875
    ## [8] 7.355825 8.852045 10.386154 7.597622 8.540947 6.287205 8.310314
    ## [15] 7.004540 11.916455 11.264865 10.769398 5.298348 7.785135

     

    • 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.