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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:40:45+00:00 2026-06-14T04:40:45+00:00

I have a large dataset and have defined outliers to be those values which

  • 0

I have a large dataset and have defined outliers to be those values which fall either above the 99th or below the 1st percentile.

I’d like to take the mean of those outliers with their previous and following datapoints, then replace all 3 values with that average in a new dataset.

If there’s anyone who knows how to do this I’d be very grateful for a response.

  • 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-14T04:40:46+00:00Added an answer on June 14, 2026 at 4:40 am

    If you have a list of indices specifying the outliers location in the vector, e.g. using:

    out_idx = which(df$value > quan0.99)
    

    You can do something like:

    for(idx in out_idx) {
      vec[(idx-1):(idx+1)] = mean(vec[(idx-1):(idx+1)])
    }
    

    You can wrap this in a function, making the bandwith and the function an optional parameter:

    average_outliers = function(vec, outlier_idx, bandwith, func = "mean") {
       # iterate over outliers
       for(idx in out_idx) {
        # slicing of arrays can be used for extracting information, or in this case,
        # for assiging values to that slice. do.call is used to call the e.g. the mean 
        # function with the vector as input.
        vec[(idx-bandwith):(idx+bandwith)] = do.call(func, out_idx[(idx-bandwith):(idx+bandwith)])
      }      
      return(vec)
    }
    

    allowing you to also use median with a bandwith of 2. Using this function:

    # Call average_outliers multiple times on itself,
    # first for the 0.99 quantile, then for the 0.01 quantile.
    vec = average_outliers(vec, which(vec > quan0.99))
    vec = average_outliers(vec, which(vec < quan0.01))
    

    or:

    vec = average_outliers(vec, which(vec > quan0.99), bandwith = 2, func = "median")
    vec = average_outliers(vec, which(vec < quan0.01), bandwith = 2, func = "median")
    

    to use a bandwith of 2, and replace with the median value.

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

Sidebar

Related Questions

I have a large dataset of philosophic arguments, each of which connect to other
I have a large dataset (202k points). I know that there are 8 values
I have a large function which has a vector defined as follows: v <-
I have a large dataset of 1.2crore rows, which is taking around 30 min
I have a very large dataset which I am currently writing out to a
Im working with a large dataset of point of interest (POI) which all have
I have two tables: p_group.full_data, which is a large dataset I'm working on (100k
I have a large dataset that load in with a fill method on page
I have a large dataset that I am dealing with in Python. It is
Imagine you have a large dataset that may or may not be filtered by

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.