Randomly assigning values to missing data

How can I randomly assign a given number of 1s and 2s to cells with missing data within a vector? I know many 1s and 2s I need (but need to randomly assign them) and I have marked cells with missing data as NA.

Thanks.

You can use the sample() function to generate a random set of 1s and 2s of a given length.

set.seed(34843)

#Make a vector with some NAs
x <- sample(c(10:13, NA), 20, replace = TRUE)

x
#>  [1] 13 12 13 13 11 NA 11 11 10 NA 13 NA 10 NA 11 11 12 12 12 11
y <- sum(is.na(x)) #count the NAs

#replace the NAs with 1 or 2
x[is.na(x)] <- sample(c(1,2), y, replace = TRUE)
x
#>  [1] 13 12 13 13 11  2 11 11 10  1 13  1 10  2 11 11 12 12 12 11

Created on 2019-03-20 by the reprex package (v0.2.1)

With this information, the only thing that comes to my mind is sampling with probabilities, to control the amounts of 1s and 2s.

sample(1:2, 10, replace = TRUE, prob = c(0.3, 0.7))
#>  [1] 2 2 1 2 2 2 2 1 2 2

Could you please turn this into a self-contained REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

Another solution controlling the exact number of 1s and 2s

set.seed(34843)
x <- sample(c(10:13, NA), 20, replace = TRUE) # Made up data with NAs
num_na <- sum(is.na(x)) # Number of NA values
num_na
#> [1] 4
num_1 = 1 # Defining the number of 1s and 2s
num_2 = num_na - num_1
x[is.na(x)] <- sample(c(rep(1,num_1), rep(2,num_2)), 4) # Replacing NA values
x
#>  [1] 13 12 13 13 11  2 11 11 10  2 13  1 10  2 11 11 12 12 12 11

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.