5 min read•august 14, 2024
R's basic arithmetic operations form the foundation of data manipulation and analysis. These tools allow you to perform calculations, compare values, and make decisions in your code. From simple math to complex conditionals, mastering these operations is crucial for effective R programming.
Understanding how to combine arithmetic and logical operations enables you to solve real-world problems. You'll use these skills to filter data, create custom functions, and implement algorithms. As you progress, you'll see how these basics underpin more advanced R programming concepts.
+
, -
, *
, and /
c(1, 2, 3) + c(4, 5, 6)
returns c(5, 7, 9)
c(1, 2, 3) + 4
returns c(5, 6, 7)
(the scalar 4 is recycled)c(1, 2, 3) * c(4, 5, 6)
returns c(4, 10, 18)
matrix(1:6, nrow = 2) / 2
divides each element of the matrix by 22 + 3 * 4
returns 14 (multiplication is performed before addition)%%
returns the remainder of the division of two numbers
10 %% 3
returns 1 (10 divided by 3 has a remainder of 1)%/%
returns the integer quotient of the division of two numbers
10 %/% 3
returns 3 (10 divided by 3 has an integer quotient of 3)[sum()](https://www.fiveableKeyTerm:sum())
, prod()
, cumsum()
, and cumprod()
for performing arithmetic operations on vectors or arrays
sum(c(1, 2, 3))
returns 6 (the sum of the elements in the vector)cumprod(c(1, 2, 3))
returns c(1, 2, 6)
(the cumulative product of the elements in the vector)log()
function computes the natural logarithm (base e) of a number or each element in a vector
log10()
for base 10 logarithm and log2()
for base 2 logarithmlog(exp(1))
returns 1 (the natural logarithm of e raised to the power of 1)exp()
function calculates the exponential (e raised to the power) of a number or each element in a vector
exp(log(10))
returns 10 (e raised to the power of the natural logarithm of 10)sqrt()
function calculates the square root of a number or each element in a vector
sqrt(16)
returns 4 (the square root of 16)sin()
, cos()
, and tan()
are available for calculating sine, cosine, and tangent, respectively
sin(pi/2)
returns 1 (the sine of 90 degrees or π/2 radians)abs()
function returns the absolute value of a number or each element in a vector
abs(-5)
returns 5 (the absolute value of -5)round()
, floor()
, and ceiling()
round(3.14)
returns 3 (3.14 rounded to the nearest integer)factorial()
function computes the factorial of a non-negative integer
factorial(5)
returns 120 (5 factorial, which is 5 * 4 * 3 * 2 * 1)&&
returns TRUE
if both operands are TRUE
, and FALSE
otherwise
FALSE
operand is encounteredTRUE && FALSE
returns FALSE
||
returns TRUE
if at least one of the operands is TRUE
, and FALSE
otherwise
TRUE
operand is encounteredTRUE || FALSE
returns TRUE
!
negates a logical value, returning TRUE
if the operand is FALSE
, and FALSE
if the operand is TRUE
!TRUE
returns FALSE
(x > 0) && (x < 10)
checks if x is between 0 and 10 (exclusive)if (x > 0) { print("Positive") } else { print("Non-positive") }
while (i <= 10) { print(i) i <- i + 1 }
if (x %% 2 == 0) { print("Even") } else { print("Odd") }
for (i in 1:10) { if (i %% 3 == 0) { print(i) } }