R:논리적인 조건을 존중하지 않는

0

질문

나는 작업으로 프로그래밍 언어입니다. 내가 노력하고 구축을 반복 수행하는 다음과 같다:

  • 1 단계:생성을 유지 두 가지 숫자는 임의의"a"와"b"까지 모두"a"와"b"보다 큰 12

  • 단계 2:얼마나 많은 임의의 숫자를 생성할 때까지 그를 위해 1 단계를 완료할

  • 3 단계:반복 1 단계와 2 단계 100 시간

이후 나는 알 수 없을 유지하는 방법을 생성하는 임의의 숫자를 조건이 충족될 때까지,나를 생성하는 대량의 임의의 숫자를 기대하는 조건을 충족(아마 더 나은 방법으로 이것을 쓰):

results <- list()


for (i in 1:100){
  
  # do until break
  repeat {
    
    # repeat many random numbers
    a = rnorm(10000,10,1)
    b = rnorm(10000,10,1)
    
    # does any pair meet the requirement
    if (any(a > 12 & b > 12)) {
      
      # put it in a data.frame
      d_i = data.frame(a,b)
      
      # end repeat
      break
    }
  }
  
  # select all rows until the first time the requirement is met
  # it must be met, otherwise the loop would not have ended
  d_i <- d_i[1:which(d_i$a > 10 & d_i$b > 10)[1], ]
  
  # prep other variables and only keep last row (i.e. the row where the condition was met)
  d_i$index = seq_len(nrow(d_i))
  d_i$iteration = as.factor(i)
e_i = d_i[nrow(d_i),]
  
  results[[i]] <- e_i
  
}

results_df <- do.call(rbind.data.frame, results)

문제가: 면,결과를 나타났는 반복이 잘못을 고려야 할 조건을 만났다,예를 들어:

head(results_df)

          a        b index iteration
4  10.29053 10.56263     4         1
5  10.95308 10.32236     5         2
3  10.74808 10.50135     3         3
13 11.87705 10.75067    13         4
1  10.17850 10.58678     1         5
14 10.14741 11.07238     1         6

예를 들어,이러한 각각의 행은 모두"a"와"b"보다 작은 12.

누군가는 이유를 알고 이것은 일어나고 있는 사람을 보여주십시오게 이 문제를 해결하는 방법?

감사합니다!

data-manipulation loops r while-loop
2021-11-24 02:45:09
1

최고의 응답

4

는 방법에 대해 이 방법은? 당신이 태그 while-loop게 그것을 사용하고 있다.

res <- matrix(0, nrow = 0, ncol = 3)    

for (j in 1:100){
  a <- rnorm(1, 10, 1)
  b <- rnorm(1, 10, 1)
  i <- 1
  while(a < 12 | b < 12) {
    a <- rnorm(1, 10, 1)
    b <- rnorm(1, 10, 1)
    i <- i + 1
  }
  x <- c(a,b,i)
  res <- rbind(res, x)
}

head(res)
      [,1]     [,2] [,3]
x 12.14232 12.08977  399
x 12.27158 12.01319 1695
x 12.57345 12.42135  302
x 12.07494 12.64841  600
x 12.03210 12.07949   82
x 12.34006 12.00365  782

dim(res)
[1] 100   3
2021-11-24 02:52:21

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................