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

The Archive Base Latest Questions

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

Another basic question from an R newbie. I have a dataset: testMeanSD. Here is

  • 0

Another basic question from an R newbie. I have a dataset: testMeanSD. Here is some relevant data, using dput() – my first time trying this for ouput, so I hope I have done it correctly:

testMeanSD <- structure(list(RT = c(1245L, 1677L, 1730L, 1066L, 994L), mean = c(1143.77777777778, 
1143.77777777778, 1143.77777777778, 1143.77777777778, 1143.77777777778
), sd = c(202.255299928596, 202.255299928596, 202.255299928596, 
202.255299928596, 202.255299928596), RT2 = c(1245L, 1677L, 1730L, 
1066L, 994L)), .Names = c("RT", "mean", "sd", "RT2"), row.names = c(NA, 
5L), class = "data.frame")

RT2 is just a duplicate of RT for me to modify. For each row, I need to alter the value of RT2 if it meets certain conditions. Otherwise RT2 stays the same as RT (or as the current value in RT2, which is the same thing). Here are the conditions:

  1. find all values in RT2 that exceed the Mean + 2.5 * SD and trim them to be equal to the Mean + 2.5 * SD

    if (RT2 > Mean + (2.5 * SD)) RT2 = Mean + 2.5 * SD

  2. find all values that are less than the Mean – 2.5 times the SD and trim them to be equal to the Mean – 2.5 * SD

    else if (RT2 < Mean – (2.5 * SD)) RT2 = Mean – 2.5 * SD

  3. leave everything else as is

    else
    RT2 = RT

I thought this would be fairly basic in R, but I simply can’t find a way to make it work. Here are some of my attempts (all failed):

First:

testMeanSD$RT2 = testMeanSD$RT
if (testMeanSD$RT2 > (testMeanSD$mean + (2.5 * testMeanSD$sd))) {
    testMeanSD$RT2 = (testMeanSD$mean + (2.5 * testMeanSD$sd))
}
else if(testMeanSD$RT2 < (testMeanSD$mean - (2.5 * testMeanSD$sd))) {
    testMeanSD$RT2 = (testMeanSD$mean - (2.5 * testMeanSD$sd))
}
else {
    testMeanSD$RT2 = testMeanSD$RT
}

Second:

ifelse(testMeanSD$RT2 > (testMeanSD$mean + (2.5 * testMeanSD$SD)), testMeanSD$RT2 <- (testMeanSD$mean + (2.5 * testMeanSD$sd)),
    ifelse(testMeanSD$RT2 < (testMeanSD$Mean - (2.5 * testMeanSD$sd)), testMeanSD$RT2 <- (testMeanSD$mean - (2.5 * testMeanSD$sd)), testMeanSD$RT2 <- testMeanSD$RT)

Third:

testMeanSD$RT2 <- ifelse(testMeanSD$RT2 > (testMeanSD$mean + (2.5 * testMeanSD$sd)), testMeanSD$mean + (2.5 * testMeanSD$sd)),
   ifelse(testMeanSD$RT2 < (testMeanSD$mean - (2.5 * testMeanSD$SD)), (testMeanSD$mean - (2.5 * testMeanSD$sd)), testMeanSD$RT2 <- testMeanSD$RT)

I looked through some related posts, and this one seems closest: Loop over rows of dataframe applying function with if-statement

But it’s not clear for me how to incorporate if then into the approaches outlined there (if not as I have them above).

Any help would be greatly appreciated. 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-10T02:29:13+00:00Added an answer on June 10, 2026 at 2:29 am

    You almost certainly want to avoid loops and if statements here in favor of vectorized conditionals and assignment.

    Let’s take your first example if (RT2 > Mean + (2.5 * SD)) RT2 = Mean + 2.5 * SD, assuming your data.frame is called dat:

    sel <- dat$RT2>dat$mean + 2.5*dat$SD # creates a boolean of length nrow(dat)
    dat$RT2[sel] <- with(dat[sel,], mean + 2.5*SD)
    

    You can use with() to save a lot of typing of “dat$“.

    N.B. I haven’t tested this since there’s no reproducible dataset. There’s almost certainly a typo somewhere!

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

Sidebar

Related Questions

another newbie-question regarding rails and bootstrap. I am using something like this: <div class=tabbable
good fellas. I have another very basic question that I need help with. I
I have just started using RadControls so this question might be basic for you,
I am a newbie at web programming. I have basic PHP programming skills. From
i have rather very basic question. I have one GUI class and another which
Very basic question I think. I have a website where some pages are likely
This is a basic question but I need some help with it. I have
I have a basic question for which I need a some clarification in it's
Sorry, another super basic ASP.NET question. this so embarrassing. I am reading the article
I've got another question regarding to basic Android programming: How can I access the

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.