# Create a random sample by ignoring missing values in an R vector.

R ProgrammingServer Side ProgrammingProgramming

#### Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

To create a random sample by ignoring missing values in an R vector, we can use sample function and the negation of is.na with vector name.

For Example, if we have a vector called X that contains some NAs then we can create a random sample of size 100 of X values by using the command given below −

sample(X[!is.na(X)],100,replace=TRUE)

## Example 1

To create a random sample by ignoring the missing values in an R vector, use the command given below −

x1<-c(NA,2,5)
sample(x1[!is.na(x1)],200,replace=TRUE)

## Output

If you execute the above given command, it generates the following Output −

[1]   5 5 5 2 2 2 5 5 5 5 2 5 2 5 5 2 2 2 2 5 2 2 5 5 5 5 2 2 5 2 5 2 2 2 2 5 2
[38]  2 2 2 2 2 2 2 2 2 5 5 2 2 2 5 5 5 5 2 5 5 2 5 2 2 2 5 5 2 5 5 5 5 2 2 2 2
[75]  2 2 2 2 5 2 5 5 2 5 2 2 5 5 5 2 2 2 2 2 2 5 5 2 2 2 5 2 5 2 2 2 2 5 2 2 2
[112] 2 5 2 5 2 5 2 2 2 5 2 2 5 5 5 2 2 5 2 5 5 2 2 5 2 2 2 5 2 2 2 2 5 5 5 2 2
[149] 5 2 2 2 2 5 2 5 5 5 5 5 5 2 5 2 5 5 2 2 5 5 5 5 2 5 5 2 5 2 5 5 2 5 5 5 2
[186] 2 5 5 2 5 2 5 5 5 5 2 5 5 5 2

## Example 2

To create a random sample by ignoring the missing values in an R vector, use the command given below −

x2<-c(NA,rnorm(5))
sample(x2[!is.na(x2)],100,replace=TRUE)

## Output

If you execute the above given command, it generates the following Output −

[1]  -0.9043329 0.4519504 0.4519504 -0.8380657 0.1107640 1.0243951
[7]  -0.8380657 0.1107640 0.4519504 0.1107640 -0.8380657 0.1107640
[13] -0.8380657 1.0243951 0.1107640 0.1107640 -0.8380657 -0.9043329
[19]  0.4519504 -0.9043329 1.0243951 -0.8380657 -0.8380657 1.0243951
[25] -0.8380657 -0.8380657 0.1107640 0.1107640 1.0243951 -0.9043329
[31]  0.4519504 0.4519504 0.4519504 -0.9043329 1.0243951 -0.9043329
[37]  1.0243951 -0.9043329 0.1107640 1.0243951 1.0243951 1.0243951
[43] -0.8380657 1.0243951 1.0243951 0.1107640 -0.8380657 -0.8380657
[49]  0.1107640 -0.9043329 0.1107640 -0.9043329 0.1107640 -0.8380657
[55]  0.4519504 0.1107640 1.0243951 0.1107640 0.1107640 0.1107640
[61] -0.8380657 -0.8380657 1.0243951 1.0243951 1.0243951 0.4519504
[67]  0.1107640 -0.9043329 1.0243951 -0.8380657 -0.8380657 -0.8380657
[73]  1.0243951 1.0243951 0.4519504 1.0243951 0.1107640 1.0243951
[79] -0.9043329 0.1107640 -0.9043329 -0.9043329 -0.9043329 -0.9043329
[85]  1.0243951 0.1107640 0.4519504 0.4519504 0.4519504 0.4519504
[91] -0.9043329 0.4519504 -0.8380657 0.1107640 0.1107640 1.0243951
[97] -0.8380657 -0.8380657 0.1107640 -0.9043329

## Example 3

To create a random sample by ignoring the missing values in an R vector, use the command given below −

x3<-c(NA,rpois(5,25))
x3

## Output

If you execute the above command, it generates the following Output −

[1] NA 25 29 20 34 30


To create a random sample by ignoring the missing values in an R vector, add the following code to the above snippet −

x3<-c(NA,rpois(5,25))
sample(x3[!is.na(x3)],200,replace=TRUE)

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1]   29 30 25 29 30 29 34 29 20 30 25 29 29 30 34 25 25 30 25 29 30 34 30 20 25
[26]  34 30 20 29 29 25 20 29 25 34 34 30 30 29 20 29 30 30 34 20 25 25 29 30 29
[51]  30 20 25 29 20 25 29 34 20 20 25 34 29 34 34 20 25 29 20 30 20 20 20 20 25
[76]  30 25 25 25 34 30 34 34 29 30 25 25 29 29 30 25 34 30 30 34 29 29 25 34 29
[101] 30 20 25 30 29 25 34 25 34 34 34 34 30 29 34 25 20 29 29 20 34 30 20 25 29
[126] 34 34 34 30 20 34 25 30 34 34 29 20 25 25 20 29 34 29 20 30 30 34 34 25 30
[151] 30 30 25 30 34 20 29 25 30 30 25 29 25 34 25 20 34 20 20 29 25 20 29 30 25
[176] 25 25 20 29 30 29 25 29 34 29 25 29 30 20 34 29 25 29 29 34 29 25 25 25 30

## Example 4

To create a random sample by ignoring the missing values in an R vector, use the command given below −

x4<-c(NA,rpois(10,500))
sample(x4[!is.na(x4)],200,replace=TRUE)

## Output

If you execute the above given command, it generates the following Output −

[1]   489 490 495 501 520 479 489 495 479 511 489 495 479 521 501 490 520 521
[19]  479 492 479 511 501 489 511 495 501 501 501 511 490 511 521 520 479 520
[37]  511 521 521 511 489 501 492 520 492 479 489 520 479 492 479 495 501 492
[55]  511 479 501 492 495 479 520 492 521 489 520 520 479 521 479 501 520 511
[73]  520 492 520 501 492 521 501 489 521 495 495 520 501 479 489 479 490 501
[91]  495 520 479 501 511 511 479 521 501 490 511 511 501 520 479 520 501 501
[109] 520 511 520 479 520 521 520 492 490 479 501 479 501 489 511 521 479 520
[127] 501 479 501 495 521 495 501 490 489 479 501 492 489 521 520 511 511 489
[145] 489 495 479 521 501 489 492 521 495 489 521 520 495 490 490 495 490 479
[163] 520 479 492 521 490 520 495 511 479 495 495 495 479 511 501 479 511 489
[181] 520 495 490 492 492 492 492 520 520 479 489 490 492 479 501 490 495 520
[199] 495 479

## Example 5

To create a random sample by ignoring the missing values in an R vector, use the command given below −

x5<-c(NA,runif(10,5,10))
sample(x5[!is.na(x5)],100,replace=TRUE)

## Output

If you execute the above given command, it generates the following Output −

[1]  7.617784 5.474288 5.461393 5.461393 9.459706 6.447721 8.498105 9.459706
[9]  7.617784 6.004828 7.004422 7.004422 7.617784 7.617784 9.459706 5.118856
[17] 6.447721 7.004422 7.004422 6.004828 9.459706 6.447721 8.498105 7.004422
[25] 7.004422 8.498105 6.447721 6.004828 9.459706 9.459706 5.474288 7.617784
[33] 5.461393 6.004828 7.617784 9.459706 8.498105 7.004422 7.004422 6.447721
[41] 5.474288 5.039730 6.004828 6.004828 7.004422 6.004828 5.118856 5.039730
[49] 5.118856 7.617784 7.004422 5.039730 6.004828 9.459706 7.004422 5.474288
[57] 6.447721 7.617784 5.474288 9.459706 5.461393 5.039730 5.461393 5.039730
[65] 7.617784 7.617784 5.039730 5.461393 6.004828 5.039730 5.461393 5.118856
[73] 6.447721 5.474288 6.447721 6.447721 5.474288 5.118856 5.039730 5.474288
[81] 5.474288 7.617784 5.461393 9.459706 5.039730 7.617784 5.461393 5.461393
[89] 9.459706 6.447721 6.004828 8.498105 7.004422 5.039730 6.004828 8.498105
[97] 8.498105 6.447721 7.004422 9.459706
Updated on 03-Nov-2021 05:41:52