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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:38:07+00:00 2026-05-26T04:38:07+00:00

I have a input vector vi with boolean values. I want to take a

  • 0

I have a input vector vi with boolean values. I want to take a random sample of size n from the vector where the value is true, so the final vector vf has these properties

  1. The lengths of the vectors are equal
    length(vf) == length(v0)

  2. vf has n true values
    n==sum(vf)

  3. The true values in vf cannot be more than those in v0
    n <= sum(v0)

  4. All the true values in vf are also true in vi

The vectors represents a selection of rows in a data frame, and this implements a stratified sample. So far I figured out how to use which() to get the row numbers, to use sample() to get a random sample, but the last part is recreating the boolean vector. There is probably a more elegant way?

For example:

n <- 1

v0 <- c(T,T,F,F)

vf <- c(T,F,F,F)

  • 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-05-26T04:38:07+00:00Added an answer on May 26, 2026 at 4:38 am

    Here’s one solution:

    # Make up some vector v0 and choose n
    v0 <- rep(c(F,T,F), 5)
    n <- 3
    
    
    # The actual code
    x <- which(v0)
    vf <- logical(length(v0))
    vf[x[sample.int(length(x), n)]] <- TRUE
    
    
    # Finally validate the result
    identical(length(vf), length(v0)) # TRUE
    all(v0[vf])  # TRUE
    sum(vf) == n # TRUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that gets a sample (an std::vector<double> ) as input and
I have 2 input variables: a vector of p-values ( p ) with N
I have saved input from a textarea element to a TEXT column in MySQL.
I have an input file that I want to sort based on timestamp which
I have a class Step derived from std::vector<unsigned int> . I need to overload
I have the input of a gamepad thumbstick that's a vector. The range of
Hi I have a problem with my code I want the input number. Instead
I have a function that processes a row vector. I want to make it
I have to create vector of a special type depending on a user input.
I have a vector<string> vectorStrings with values: ta, bc, ac, st, cer, cda .

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.