R: Checking for an Event Between Increasing Sets of Rows

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!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật