Hey guys, what’s up? I have found that, many of you want to get answer about what is %% function in r. So by this tutorial I am going to teach you about %% operator in r. Let’s start—
Actually, %% is call modulo/ modules operator. In mathematics, we can use only modulo. But in R programming language we write two modulo together. For double arguments, %% can be subject to catastrophic loss of accuracy if x is much larger than y, and a warning is given if this is detected. There is a function of it, called mod(arg1,arg2) function.
Here,
- arg1– is a numerical value or numerical vector.
- arg2– is also a numerical value or numerical vector.
I am going to clarify it properly, so that it make more understandable for you.
è
mod(x, y) = x mod y
= x %% y
= remainder of x divided by y
è
mod(x, y) = x - y * floor(x/y)
Some mathematical examples of %% function in r
> 7 %% 2 [1] 1 > 11.8 %% 5 [1] 1.8 > 10 %% 10 [1] 0 > 20 %% 10 [1] 0 > 10 %% 20 [1] 10 > 2 %% 8 [1] 2
Time difference example of %% function in r
> # To access the help file for %% and %/%, > # use acute accents around %% > ?`%%` > > > #=====> See txtProgressBar Post <=====# > SEQ <- seq(1,100000) > TIME <- Sys.time() > for(i in SEQ){ + Sys.sleep(0.00002) + } > Sys.time() - TIME Time difference of 3.872829 secs > > pb <- txtProgressBar(1, 100000, style=3) > TIME <- Sys.time() > for(i in SEQ){ + Sys.sleep(0.00002) + setTxtProgressBar(pb, i) + } |===================================================| 100% > Sys.time() - TIME Time difference of 8.296607 secs > > pb <- txtProgressBar(1, 100000, style=3) > TIME <- Sys.time() > for(i in SEQ){ + Sys.sleep(0.00002) + if(i %% 1000 == 0){ + setTxtProgressBar(pb, i) + } + } |===================================================| 100% > Sys.time() - TIME Time difference of 4.218381 secs > 2 %% 8 [1] 2
So guys, that’s all about what is %% function in r. Later we will discuss another topic of r programming language. Till then, take care. Happy Coding