I have this code that produces a data frame in R (corresponding to repetitions of a random dice rolling experiment until two consecutive 6’s are obtained – source: R: Recursively checking dice combinations):
sides <- 1:6
rolls <- sample(sides, size = 100, replace = TRUE)
counts <- c()
position <- 1
while (position < length(rolls)) {
# check if the current and next roll are both 6
if (rolls[position] == 6 & rolls[position + 1] == 6) {
counts <- c(counts, position)
#move to the next position
position <- position + 2
} else {
position <- position + 1
}
}
# result data frame
result <- data.frame(current_occurrence = integer(), absolute_position = integer(), positions_away_from_current_occurence = integer(), six_six = character())
# Loop over all occurrences of (6,6)
for (i in 1:(length(counts)-1)) {
# Get the current occurrence
current_occurrence <- counts[i]
# Initialize j
j <- 2
# Loop until we reach the next occurrence of (6,6) or the end of the rolls
while ((current_occurrence + j <= counts[i+1])) {
# Check if (6,6) occurred at (current_occurrence + j - 1, current_occurrence + j)
six_six <- if (rolls[current_occurrence + j - 1] == 6 & rolls[current_occurrence + j] == 6) "yes" else "no"
# add the result to the data frame
result <- rbind(result, data.frame(current_occurrence = i, absolute_position = current_occurrence, positions_away_from_current_occurence = j, six_six = six_six))
j <- j + 1
}
}
The results look like this:
print(result)
current_occurrence absolute_position positions_away_from_current_occurence six_six
1 1 4 2 no
2 1 4 3 no
3 1 4 4 no
4 1 4 5 no
5 2 9 2 yes
6 2 9 3 no
7 2 9 4 no
8 2 9 5 no
9 2 9 6 no
10 2 9 7 no
I am trying to accomplish the following task:
- Isolate all rows where current_occurence=1 . Call the first row as
b*
- Check whether six_six=yes at (b*, b* + 1)
- Check whether six_yes = yes at (b*, b* +2)
- Repeat this process until you reach the last row in current_occurence=1
- Repeat this process for all values of current_occurence
In the end I am trying to produce a data frame that looks like this:
#sample result of desired output:
current_occurence b_star after_b_star six_six
1 1 1 no
1 1 2 no
1 1 3 no
1 1 4 no
1 1 5 yes
2 1 1 no
2 1 2 no
2 1 3 no
2 1 4 no
2 1 5 no
My friend and I spent some time yesterday trying to write the code for this without getting confused within the loops and subloops:
desired_result <- data.frame(current_occurrence = integer(), b_star = integer(), after_b_star = integer(), six_six = character())
# loop over all unique occurrences
for (current_occurrence in unique(result$current_occurrence)) {
# subset for the current occurrence
subset_result <- result[result$current_occurrence == current_occurrence, ]
b_star <- subset_result[1, ]
# all rows after b_star
for (i in 2:nrow(subset_result)) {
# check if six_six is "yes" at (b_star, b_star + i)
six_six <- if (subset_result[i, "six_six"] == "yes") "yes" else "no"
# append
desired_result <- rbind(desired_result, data.frame(current_occurrence = current_occurrence, b_star = 1, after_b_star = i - 1, six_six = six_six))
}
}
print(desired_result)
The output is in the correct format, but we are not sure the code is doing what we want it to do:
current_occurrence b_star after_b_star six_six
1 1 1 no
1 1 2 no
1 1 3 no
2 1 1 no
2 1 2 no
2 1 3 no
2 1 4 no
Is there a standard way to do these kinds of recursive tasks? Can someone please help?
Thanks!