简体   繁体   中英

Double for-loop issues in R

I am trying to run a double-loop and run different regression models. (I save the results of each regression and use it to cross-validate, but that is not my issue here.)

My code is as follows:


hr <- seq(50)

# testing ranges:
for (j in 1:10) {

  begin <- (25 - j)

  for (k in 1:10) {

  end <- (25 + k)

  # dummies
  z <- as.factor(ifelse(hr>=begin & hr<=end, hr, NA))
  z2 <- ifelse(begin==end, NA, z)  #this is the problematic part

  # regression here.....

  # save results here....

  }
} 

I create variable begin and end. When hr is between begin and end, then they get the value hr, if not NA. This works fine. My problem is with the next like. I would like all of the values to be NA if begin==end. This doesn't work. It gives me a variable of NAs even if begin~=end. What am I doing wrong?

Here is an option with no loops. Instead, we expand_grid to get every combo of j and k. Then we calculate beginning and end. Lastly, we map out your conditional statements on a nested vector. The results are saved in a column called hr , which is a list of vectors. The advantage here is that you can map out your regressions in the dataframe, which should save you time and organization.

library(tidyverse)

hr <- seq(50)

output <- expand_grid(j=1:10, k = 1:10) %>%
  mutate(begin = (25 - j),
         end = (25 + k),
         hr = list(hr),
         hr = pmap(list(begin, end, hr), 
                   ~case_when(
                     ..1 == ..2 ~ ..3*NA,
                     ..3>=..1 & ..3<=..2 ~ ..3,
                     T ~ NA_integer_
                   )))

output
#> # A tibble: 100 x 5
#>        j     k begin   end hr        
#>    <int> <int> <dbl> <dbl> <list>    
#>  1     1     1    24    26 <int [50]>
#>  2     1     2    24    27 <int [50]>
#>  3     1     3    24    28 <int [50]>
#>  4     1     4    24    29 <int [50]>
#>  5     1     5    24    30 <int [50]>
#>  6     1     6    24    31 <int [50]>
#>  7     1     7    24    32 <int [50]>
#>  8     1     8    24    33 <int [50]>
#>  9     1     9    24    34 <int [50]>
#> 10     1    10    24    35 <int [50]>
#> # … with 90 more rows


#first 5 vectors to show it worked
output$hr[1:5]
#> [[1]]
#>  [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 24 25
#> [26] 26 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
#> 
#> [[2]]
#>  [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 24 25
#> [26] 26 27 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
#> 
#> [[3]]
#>  [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 24 25
#> [26] 26 27 28 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
#> 
#> [[4]]
#>  [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 24 25
#> [26] 26 27 28 29 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
#> 
#> [[5]]
#>  [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 24 25
#> [26] 26 27 28 29 30 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM