Request #2 - An important part of this course is to apply the knowledge you have learned. For this reason, we would like you to solve the following assignment using R:
“Write an R script (R code) that creates a text file containing the results of 10 rolls of a pair of dice.”
The R script should be easy to use (user friendly) and flexible for extensions (e.g. more rolls, more dices, or flexible output). Think about ways to implement reproducibility, if possible? We don’t need the perfect solution, partial solutions are fine. If the task is beyond your R skills, a step-by-step description of a solution would also be fine. Please send your solution (zipped) via e-mail directly to me using subject: GDA23: Request #2.
A simple version in R. It works but has limits.
# GDA: Rolling Dice Script
# Author: Jean-Claude Walser
# Date: 17.06.23
## Roll the dice function
roll <- function() {
return(sample(1:6, 1, replace = TRUE))
}
## Set number of rolls
number_of_rolls <- 10
## Dice 1
result_dice1 <- replicate(number_of_rolls, roll())
## Dice 2
result_dice2 <- replicate(number_of_rolls, roll())
## Output Results
cat("Dice 1:", result_dice1, "\n")
## Dice 1: 3 2 5 5 4 4 2 1 6 4
cat("Dice 2:", result_dice2, "\n")
## Dice 2: 3 6 3 3 4 2 2 1 6 1
Similar to version 1 but it is reproducible and is writing the results into a file.
# GDA: Rolling Dice Script
# Author: Jean-Claude Walser
# Date: 17.06.23
## Set seed for reproducibility
set.seed(007)
## Roll the dice function
roll <- function() {
return(sample(1:6, 1, replace = TRUE))
}
## Set number of rolls
number_of_rolls <- 10
# Dice 1
result_dice1 <- replicate(number_of_rolls, roll())
## Dice 2
result_dice2 <- replicate(number_of_rolls, roll())
## Show results
ResultDice1 <- cat("Dice 1:", result_dice1, "\n")
## Dice 1: 2 3 4 2 2 6 3 6 3 2
ResultDice2 <- cat("Dice 2:", result_dice2, "\n")
## Dice 2: 4 3 4 2 6 6 3 5 4 3
## Write results in file
write(paste(result_dice1,result_dice2, sep = "-"), file = "Dice2.txt", ncolumns = 10)
Similar to version 1 but in tidyverse.
# GDA: Rolling Dice Script
# Author: Jean-Claude Walser
# Date: 19.06.23
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Set seed for reproducibility
set.seed(007)
## Roll the dice function
roll <- function() {
return(sample(1:6, 1, replace = TRUE))
}
## Set number of rolls
number_of_rolls <- 10
# Dataframe for the rols
Results_Rolls <- tibble(
Dice1 = replicate(number_of_rolls, roll()),
Dice2 = replicate(number_of_rolls, roll())
)
# Print results
print(Results_Rolls)
## # A tibble: 10 × 2
## Dice1 Dice2
## <int> <int>
## 1 2 4
## 2 3 3
## 3 4 4
## 4 2 2
## 5 2 6
## 6 6 6
## 7 3 3
## 8 6 5
## 9 3 4
## 10 2 3
There is a package TeachingDemo
and it contains a
function dice.
# GDA: Rolling Dice Script
# Author: Jean-Claude Walser
# Date: 19.06.23
require(TeachingDemos)
## Loading required package: TeachingDemos
write.table(TeachingDemos::dice(10,2), "dice2x10.txt")