Youtube channel: https://www.youtube.com/c/TechAnswers88

Direct link to the video

https://youtu.be/ANWWmEpI9wU

Generate a random number. We can set a seed by the set.seed(n) command where n is any integer.

set.seed(123)

Each time you run your script you will realise that the random numbers being created are different but you would also notice that you would get the same values each time. This is useful when you want your script to be repeatable and if you give your script to someone else they will be able to generate the same random numbers and achieve the same results.

# Generate one random number
# What does the set.seed do
set.seed(123)
runif(1) # By default the numbers are between 0 and 1
## [1] 0.2875775
runif(1) # By default the numbers are between 0 and 1
## [1] 0.7883051
runif(1) # By default the numbers are between 0 and 1
## [1] 0.4089769
# Generate two random numbers
runif(2) # By default the numbers are between 0 and 1
## [1] 0.8830174 0.9404673
# Generate threee random numbers
runif(3) # By default the numbers are between 0 and 1
## [1] 0.0455565 0.5281055 0.8924190
# Decimal values being generated
runif(100, min=0, max=101)
##   [1]  55.69493646  46.11808827  96.64016788  45.78674978  68.43463418
##   [6]  57.83597360  10.39539295  90.88232201  24.85486117   4.24801289
##  [11]  33.11999265  96.40486856  89.84347092  69.97314402  64.69118819
##  [16] 100.42124744  66.22628571  71.56157728  54.95066850  60.00834407
##  [21]  29.20513347  14.85847838  97.26544749  91.13220356  69.76123312
##  [26]  80.34220919   2.48598214  48.25739308  76.60441329  21.85720152
##  [31]  32.13628177  23.39420432  14.42280226  41.86917992  41.78615696
##  [36]  37.25339054  15.39691952  14.01941241  23.53644404  47.06220748
##  [41]  26.86323668  86.64059925   4.62894783  44.66220749  80.69140941
##  [46]  12.31182526  56.65574636  20.85967035  12.88069667  76.08409429
##  [51]  90.39958127  37.82074036  67.17663466   9.57890675  38.78093342
##  [56]  27.71274810  82.27864393  45.30015048  81.81649966  82.05134046
##  [61]  80.22857443  44.42300045  76.20199102  63.55133429  71.72842254
##  [66]   0.06310211  48.00697398  22.23200740  38.36147031  61.88987133
##  [71]  35.53158883  11.22467786  24.60556674  67.47361433  42.18232475
##  [76]  79.60777924  10.38932907  43.92416689  99.48065498  90.19816255
##  [81]  89.53337514  17.68031768  13.20026485  65.96329443  34.69516370
##  [86]  66.33257092  32.35769749  18.95680305  79.01172443   9.45309366
##  [91]  47.14468320  51.66205145  60.59888489  33.61517757  49.34991640
##  [96]  96.40185658  48.77314211  89.92537243  92.35825688  61.48223321
# Integer values 
as.integer(runif(100, min=0, max=101))
##   [1] 41 14 94 30  6 95 72 14 55 96 59 40 65 32 31 22 37 99 15  9 14 69 62 90 67
##  [26] 74 52 66 83 79 98 44 31 41  1 18 85 23 24  7 24 73 85 50 39 24 11 39 57 21
##  [51] 44 22 50 35 65 37 35 53 74 22 41 26 63 18 87 75 67 62 37 53 88 58 84 31 71
##  [76] 26 60 48 26 57 92 91 27 32 99 62 94 47 41 66 15 57 24 97 60 52 40 88 36 29
# using floor command to generate integers
#The R floor method is one of the R Math functions, which is to return the largest integer value.

# You would notic that your values will be between 0 to 100 .
# As a value of 100.9 will be 100
floor(runif(100, min=0, max=101))
##   [1] 17 17 48 25 21 68  4 70 35 41 82 92 28 97 73 69  5 39 48 56 70 92 62 43 54
##  [26]  5 26 40 19 84 15 81 55 66 17 63 31 73 40 97 97 73 25 22 59 27 53 79 16 40
##  [51] 47 87 93 89 68 95 52 58 33 35  2 50 87  0  7 16 77 74 98 47  7 65 76 13 40
##  [76] 22  5 39  6 22  5 67 30 10  7 88 76 82 99 10 10 80 79  0 78 73 63 48 15  0

Generate 100 random numbers with replace = TRUE (The result may use the same number more than once)

# Get a vector of n numbers from 0 to 100
sample(1:100, 100, replace=TRUE)
##   [1] 85  7 60 26 41 84  6 94 31 93 17 64 37 57 20 35 89 33 66  4 74 97  5 25  8
##  [26] 55 89 85 45 18 42 31  6 71 61 48 17 45 92 63 53 53 63 71 84 82 93 17 97  2
##  [51] 49  2 13 24 49 67 82  2 37 63 76 13 42 61 70 38 64 80 99 36 91 91 35 87 67
##  [76] 95 48 66 23 67  9 61 39 94 31 94 73 36 98 22 44 41 92 93 43 39 37 33 83 12

Generate 100 random numbers with replace = FALSE (The result will not have the same number twice)

# Get a vector of n numbers from 0 to 100
sample(1:100, 100, replace=FALSE)
##   [1]  98  21  81  76  94  35  30  89  50  47  67  82 100  42  40  87  26  13
##  [19]  20  93  41   8  39  74  63  11   2  19  83  31  96  22  16  36  92  33
##  [37]  68  28  49  12  53  97  77  66  61  91  51  60   6  64  15  73   3  46
##  [55]  65  55  75  90  10  24  59  78  18  17  88   9  44  79  38   5  29  32
##  [73]  52  85  84  95  34  23  86  56  62   4  48  14  45  69  43   7  27  80
##  [91]  54  37  58   1  57  70  25  72  71  99
# drawing samples from a database
library(dplyr)

#set.seed(123)
dplyr::sample_n(mtcars, 10)
dplyr::sample_n(mtcars, 10, replace=TRUE)
nrow(mtcars)
## [1] 32
mtcars%>%sample_frac(.5)