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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:06:24+00:00 2026-06-11T03:06:24+00:00

Im writing some code in R whereby i need to generate a data set

  • 0

Im writing some code in R whereby i need to generate a data set from specific criteria given they meet certain conditions.

I have three probabilities
A – 0.423, B – 0.324 and C- 0.253.

I want to run a random generated sample runif(50, 0, 1).

If the number generated lies between 0 and 0.423, i want to generate a value from rnorm(50, 25, 4),
if its between 0.423 and 0.747, i want to generate a value from rnorm(50, 28, 4.5)
and finally if its between 0.747 and 1, i want to generate a value from rnorm(50, 30, 5).

I was trying to do this using some sort of compound ifelse function but to no avail.

Any suggestions?

Cheers

  • 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-11T03:06:25+00:00Added an answer on June 11, 2026 at 3:06 am

    As you suspected, this can be done using ifelse:

    u = runif(50, 0, 1)
    values = ifelse(u < .423, rnorm(50, 25, 4),
                    ifelse(u < .747, rnorm(50, 28, 4.5),
                           rnorm(50, 30, 5)))
    

    This works because ifelse can operate along vectors.

    You might prefer this method, especially if you end up having more than three sections to break it into:

    means = c(25, 28, 30)
    vars = c(4, 4.5, 5)
    probs = c(.423, .324, .253)
    samples = sample(1:3, 50, replace=TRUE, prob=probs)
    values = rnorm(50, means[samples], vars[samples])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help with writing some code that will create a random number from
I am writing some code to generate an opml file from a list of
I'm writing some code that will take a screenshot of another application, given its'
I have some trouble writing some code (I might be tired), so I need
I am writing some code to generate anchor elements via Ajax call to the
I've been writing some code that extracts the main textual content from web pages.
I'm writing some code to load and parse HTML docs from the web. I'm
I am writing some code for driving Internet Explorer from a Perl 5 program
I'm writing some code where the UI thread need to communicate with the background
I'm writing some code and need to search for some kinds of symbols in

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.