YouTip LogoYouTip

R Func Sample

# Sampling with replacement (each independent, may repeat) set.seed(123) dice_rolls <- sample(1:6, size = 10, replace = TRUE) print("Roll 10 dice:") print(dice_rolls)

The output of running the above code is:

 "Winning Numbers:"
  31 79 51 14 67 42 50 43 97 25 "Roll 10 dice:"
  6 1 4 2 2 1 4 5 5 1

You can set unequal sampling probabilities through the prob parameter:

Example

# Simulate a biased coin (heads probability 0.7, tails 0.3)
set.seed(123)
biased_coin <- sample(c("Example", "Counterexample"), size = 100,
              replace = TRUE, prob = c(0.7, 0.3))
print("100 times biased coin result ratio:")
print(prop.table(table(biased_coin)))

The output of running the above code is:

 "100 times biased coin result ratio ratio:"
Counterexample & Example 
0.27 0.73 

Image 4: R Language Example R Language Examples

← R Func ScaleR Func Rpois β†’