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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:44:31+00:00 2026-05-27T08:44:31+00:00

I have a vector A which contains zeros and ones. I would like to

  • 0

I have a vector A which contains zeros and ones. I would like to randomly change n percent of the ones to zero. Is this the best way to do it in R (10% change):

for (i in 1:length(A)) 
{
    if(A[i] > 0)
    {
        if(runif(1) <= 0.1)
        {
            A[i] = 0
        }
    }
}

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-05-27T08:44:31+00:00Added an answer on May 27, 2026 at 8:44 am

    You can do this without using the for loops and if statements:

    ##Generate some data
    R> A = sample(0:1, 100, replace=TRUE)
    ##Generate n U(0,1) random numbers
    ##If any of the U's are less then 0.1
    ##Set the corresponding value in A to 0
    R> A[runif(length(A)) < 0.1] = 0
    

    The other point to note, is that you don’t have to do anything special for values of A that actually equal 0, as the probability of change a 1 to a 0 is still 0.1.

    As Hadley points out, your code doesn’t randomly change 10% of 1’s to 0. If that is really your intention, then:

    ##Select the rows in A equal to 1
    R> rows_with_1 = (1:length(A))[A==1]
    ##Randomly select a % of these rows and set equal to zero
    ##Warning: there will likely be some rounding here
    R> A[sample(rows_with_1, length(rows_with_1)*0.1)] = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long vector x, and another v, which contains lengths. I would
I have an (access) database table which contains data I would like to populate
I have a vector that contains zlib-compressed (deflated) data. I would like to decompress
So I have a container(any kind, probably std::map or std::vector) which contains objects of
I have a set of objects in a Vector from which I'd like to
I have the following function (which worked in Visual Studio): bool Plane::contains(Vector& point){ return
I have a 2D vector which contains the coordinates which I want to represent
I have a vector which contains a list of Hash maps in Clojure and
I have a vector, call it x, which contains very small numbers that I
I have a vector-like class that contains an array of objects of type T

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.