1

I came across below code

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v/t)
print(v%/%t)

In what way 3rd and 4th lines of code are different?

3
  • / is division while %/% is integer division Commented Jan 3, 2020 at 10:35
  • I do not understand how you can ask if the 3rd and 4th code lines are the same, their results are different. Commented Jan 3, 2020 at 10:45
  • @Monic I first saw the example of two vectors which has only 0's and 1's .Since I was new to r programming, I am in tense and immediately posted this question. Commented Jan 3, 2020 at 10:54

2 Answers 2

5

/ does the usual division.

Regarding %/%, the documentation states:

%% indicates x mod y and %/% indicates integer division. It is guaranteed that x == (x %% y) + y * ( x %/% y ) (up to rounding error) unless y == 0 […]

To find the documentation for such operators, enter the following on the R console:

help("%/%")
Sign up to request clarification or add additional context in comments.

2 Comments

I recommend the help function with standard evaluation: help("%/%")
I prefer it for didactic reasons ... With ? you need to know when to use backtics and stuff. Personally, I use RStudio's documentation pane.
3

Both are arithmetic operators. The first one is division, the second is integer division. See here: https://www.statmethods.net/management/operators.html

> 10/3
[1] 3.333333
> 10%/%3
[1] 3

In addition, there is also modulus division (x %% y)

> 10%%3
[1] 1

That's all I know about division operators :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.