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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:01:06+00:00 2026-06-13T09:01:06+00:00

I am trying to step through a vector to find the outliers using IQR

  • 0

I am trying to step through a vector to find the outliers using IQR to calculate a range. When I run this script looking for values to the right of the IQR I get results and when I run to the left I get the error: missing value where TRUE/FALSE needed. How can I scrub out the true and false in my dataset?
here is my script:

data = c(100, 120, 121, 123, 125, 124, 123, 123, 123, 124, 125, 167, 180, 123, 156)
Q3 <- quantile(data, 0.75) ##gets the third quantile from the list of vectors
Q1 <- quantile(data, 0.25) ## gets the first quantile from the list of vectors
outliers_left <-(Q1-1.5*IQR(data)) 
outliers_right <-(Q3+1.5*IQR(data))
IQR <- IQR(data)
paste("the innner quantile range is", IQR)
Q1 # quantil at 0.25
Q3 # quantile at 0.75
# show the range of numbers we have
paste("your range is", outliers_left, "through", outliers_right, "to determine outliers")
# count ho many vectors there are and then we will pass this value into a loop to look for 
# anything above and below the Q1-Q3 values
vectorCount <- sum(!is.na(data))
i <- 1
while( i < vectorCount ){
i <- i + 1
x <- data[i]
# if(x < outliers_left) {print(x)} # uncomment this to run and test for the left
if(x > outliers_right) {print(x)}
}

and the error I get is

[1] 167
[1] 180
[1] 156
Error in if (x > outliers_right) { : 
missing value where TRUE/FALSE needed

as you can see if you run this script, it is finding my 3 outliers on the right and also throws the error, but when I run this again on the left of my IQR, and I do have an outlier of 100 in the vector, I just get the error without other results being displayed.
How can I fix this script? any help greatly appreciated. I’ve been scouring the web and my books for days on how to fix this.

  • 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-06-13T09:01:08+00:00Added an answer on June 13, 2026 at 9:01 am

    As noted in the comments, the error is due to the way you’ve constructed your while loop. At the last iteration, i == 16 though there are only 15 elements to process. Changing from i <= vectorCount to i < vectorCount fixes the problem:

    i <- 1
    while( i < vectorCount ){
      i <- i + 1
      x <- data[i]
      # if(x < outliers_left) {print(x)} # uncomment this to run and test for the left
      if(x > outliers_right) {print(x)}
    }
    #-----
    [1] 167
    [1] 180
    [1] 156
    

    However, this is really not how R works and you’ll soon be frustrated at how long that code will take to run for any appreciable sized data. R is “vectorized” meaning that you can operate on all 15 elements of data at once. To print your outliers, I’d do this:

    data[data > outliers_right]
    #-----
    [1] 167 180 156
    

    Or to get all of them at once using the OR operator:

    data[data< outliers_left | data > outliers_right]
    #-----
    [1] 100 167 180 156
    

    For a little context, The above logical comparisons create a boolean value for each element of data and R only returns those that are TRUE. You can check this for yourself by typing:

    data > outliers_right
    #----
     [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE
    

    The [ bit is actually an extraction operator, used to retrieve a subset of a data object. See the help page for some good background ?"[".

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

Sidebar

Related Questions

I'm using Firefox/Firebug, trying to step through some of the jquery.validate() callbacks. Why do
Currently I'm trying to populate a site dynamically. When I step through the code,
I have been trying to find some step by step guidance on how to
I'm trying to follow this tutorial on Seesaw Window Building . During the step
I have a vector of structs, with the structs looking like this: struct myData{
I am trying to be able to step through the code of a Hudson
I'm using the gdb debugger to step through my code to verify my logic
So I'm trying to work my way through this paper , which is primarily
I'm trying to step through the Data-Driven Report Subscription setup, but I can't get
I'm trying to debug and step through an Android application that segfaults. I've tried

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.