[英]Repeat a function until conditions are met in r using a while loop
這可能不是最有效的方法,但是我已經在一本書的幫助下為水果機(可以工作)創建了一個代碼(不確定我是否可以共享標題,但是這樣做非常有用)。新手)。 但是,此代碼非常簡單,它運行一個函數,該函數依次調用其他兩個函數:
pull()
通過從預定向量中隨機選擇字符來模擬車輪機制。 prize()
分析來自pull()
的響應,以使用查找表為符號添加值。 該代碼有效,但我想對其進行修改,以使該代碼一直運行到到達三顆鑽石的累積獎金為止。 "DD"
。 我還希望它量化所有其他響應,直到達到目標為止,因此我嘗試了一個while循環,該循環似乎什么也沒有產生:
play2 <- function(){
response <- pull()
# I put the while loop straight after calling the first function,
# hoping that if the condition is not met, then the first function
# runs again, creating a new "response" until conditions are met.
while (sum(response == "DD") != 3) {
# I set the condition so that if the jackpot is not achieved, the
# while loop causes the other responses to be investigated. It
# it determines this by counting booleans.
# The following assignments are designed to begin the count for how
# many responses that are considered prizes occur, and how often no
# prize occurs.
cherry_prize <- 0
B_prize <- 0
BB_prize <- 0
BBB_prize <- 0
seven_prize <- 0
no_prize <- 0
if ("C" %in% response){
cherry_prize <- cherry_prize + 1
# A cherry prize occurs if any "C" is returned. The following
# statements will asses the other non-jackpot three of a kinds.
}
else if (sum(response == "B") != 3) {
B_prize <- B_prize + 1
}
else if (sum(response == "BB") != 3) {
BB_prize <- BB_prize + 1
}
else if (sum(response == "BBB") != 3) {
BBB_prize <- BBB_prize + 1
}
else if (sum(response == "7") != 3) {
seven_prize <- seven_prize + 1
}
else {
no_prize <- no_prize + 1
}
}
# After the jackpot is achieved, the number of other prizes (or lack
# thereof) that were returned are quantified, prior to the jackpot
# were won. The jackpot triplicate would then also be returned and
# quantified.
print(cherry_prize)
print(B_prize)
print(BB_prize)
print(BBB_prize)
print(seven_prize)
print(no_prize)
print(response)
# The last function quantifies the jackpot, once it has been reached.
prize(response)
}
希望我提供了足夠的信息。 非常感謝所有幫助,並希望它可以幫助其他人。
感謝您以上的建議。 因此,我按照說明進行操作,並遇到警告,該警告阻止該功能運行。
Error in symbols == "DD" :
comparison (1) is possible only for atomic and list types
因此,我再次認為我使用的編碼方法不是最有效的方法,但是我使用as.list()
對此進行了as.list()
。
play2 <- function(){
runs <- 0
cherry_prize <- 0
B_prize <- 0
BB_prize <- 0
BBB_prize <- 0
seven_prize <- 0
no_prize <- 0
while (sum(as.list(response) == "DD") != 3) {
runs <- runs + 1
response <- pull()
if ("C" %in% response){
cherry_prize <- cherry_prize + 1
}
else if (sum(as.list(response) == "B") != 3) {
B_prize <- B_prize + 1
}
else if (sum(as.list(response) == "BB") != 3) {
BB_prize <- BB_prize + 1
}
else if (sum(as.list(response) == "BBB") != 3) {
BBB_prize <- BBB_prize + 1
}
else if (sum(as.list(response) == "7") != 3) {
seven_prize <- seven_prize + 1
}
else {
no_prize <- no_prize + 1
}
}
print(runs)
print(cherry_prize)
print(B_prize)
print(BB_prize)
print(BBB_prize)
print(seven_prize)
print(no_prize)
print(response)
score(response)
}
這給出了輸出:
> play2()
[1] 430118
[1] 12526
[1] 411199
[1] 6393
[1] 0
[1] 0
[1] 0
[1] "DD" "DD" "DD"
[1] 800
我的代碼中未顯示的某些內容正在影響我的BBB,7且沒有獎金。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.