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

  • Home
  • SEARCH
  • 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 9206565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:12:10+00:00 2026-06-18T00:12:10+00:00

I’m so new to R that I’m having trouble finding what I need in

  • 0

I’m so new to R that I’m having trouble finding what I need in other peoples’ questions. I think my question is so easy that nobody else has bothered to ask it.

What would be the simplest code to create a new data frame which excludes data which are univariate outliers(which I’m defining as points which are 3 SDs from their condition’s mean), within their condition, on a certain variable?

I’m embarrassed to show what I’ve tried but here it is

greaterthan <- mean(dat$var2[dat$condition=="one"]) + 
               2.5*(sd(dat$var2[dat$condition=="one"]))
lessthan    <- mean(dat$var2[dat$condition=="one"]) -
               2.5*(sd(dat$var2[dat$condition=="one"]))   

withoutliersremovedone1 <-dat$var2[dat$condition=="one"] < greaterthan

and I’m pretty much already stuck there.

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-06-18T00:12:11+00:00Added an answer on June 18, 2026 at 12:12 am
    > dat <- data.frame(
                        var1=sample(letters[1:2],10,replace=TRUE),
                        var2=c(1,2,3,1,2,3,102,3,1,2)
                       )
    > dat
       var1 var2
    1     b    1
    2     a    2
    3     a    3
    4     a    1
    5     b    2
    6     b    3
    7     a  102 #outlier
    8     b    3
    9     b    1
    10    a    2
    

    Now only return those rows which are not (!) greater than 2 absolute sd‘s from the mean of the variable in question. Obviously change 2 to however many sd‘s you want to be the cutoff.

    > dat[!(abs(dat$var2 - mean(dat$var2))/sd(dat$var2)) > 2,]
       var1 var2
    1     b    1
    2     a    2
    3     a    3
    4     a    1
    5     b    2
    6     b    3 # no outlier
    8     b    3 # between here
    9     b    1
    10    a    2
    

    Or more short-hand using the scale function:

    dat[!abs(scale(dat$var2)) > 2,]
    
       var1 var2
    1     b    1
    2     a    2
    3     a    3
    4     a    1
    5     b    2
    6     b    3
    8     b    3
    9     b    1
    10    a    2
    

    edit

    This can be extended to looking within groups using by

    do.call(rbind,by(dat,dat$var1,function(x) x[!abs(scale(x$var2)) > 2,] ))
    

    This assumes dat$var1 is your variable defining the group each row belongs to.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I know there's a lot of other questions out there that deal with this
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported

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.