New to Rstudio need help understanding loops and useing %%

You start with the following code and then complete the next lines:
n <- 60
Print odd numbers from 60 to 90 using repeat loop. Hint. You can use modulo (%%)

could someone explain to me how I would go about using %%

Hi, and welcome!

For future reference, please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers. Also check homework policy

4 %% 2
#> [1] 0
4 %% 3
#> [1] 1

Created on 2020-03-19 by the reprex package (v0.3.0)

For future reference, an alternative to loop

seq(from = 60, to = 90)
#>  [1] 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#> [26] 85 86 87 88 89 90

Created on 2020-03-19 by the reprex package (v0.3.0)

Also for future reference, a more R-like way of doing this is chaining functions. Just keep it in mind for later.

suppressPackageStartupMessages(library(dplyr)) 
digits <- seq(from = 60, to = 90)
obj <- digits %% 2
digits <- as.data.frame(cbind(digits, obj))
digits %>% filter(obj == 1) %>% select(digits)
#>    digits
#> 1      61
#> 2      63
#> 3      65
#> 4      67
#> 5      69
#> 6      71
#> 7      73
#> 8      75
#> 9      77
#> 10     79
#> 11     81
#> 12     83
#> 13     85
#> 14     87
#> 15     89

Created on 2020-03-19 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.