Numbers are one of the most basic and important data types in R. They represent numeric values and can be used for calculations, data analysis, and statistical operations.
5
, -10
).3.14
or -0.5
. This is the default numeric type in R.3 + 2i
.You can check the type of a number using functions like typeof()
or class()
:
x <- 10 typeof(x) # "double" (by default numbers are double) class(x) # "numeric" y <- 10L # L suffix makes it integer typeof(y) # "integer" class(y) # "integer" z <- 3 + 2i typeof(z) # "complex" class(z) # "complex"
Simply assign numbers to variables:
a <- 25 # numeric (double) by default b <- 12L # integer (with L suffix) c <- 3.14159 # numeric with decimal d <- -7.5 # negative numeric e <- 1+4i # complex number
Operation | Symbol | Example | Result |
---|---|---|---|
Addition | + | 5 + 3 |
8 |
Subtraction | - | 5 - 3 |
2 |
Multiplication | * | 5 * 3 |
15 |
Division | / | 5 / 3 |
1.6667 |
Exponentiation | ^ | 5 ^ 3 |
125 |
Modulo (Remainder) | %% | 5 %% 3 |
2 |
Integer Division | %/% | 5 %/% 3 |
1 |
num1 <- 10 num2 <- 3 sum <- num1 + num2 difference <- num1 - num2 product <- num1 * num2 quotient <- num1 / num2 power <- num1 ^ num2 remainder <- num1 %% num2 int_div <- num1 %/% num2 print(paste("Sum:", sum)) print(paste("Difference:", difference)) print(paste("Product:", product)) print(paste("Quotient:", quotient)) print(paste("Power:", power)) print(paste("Remainder:", remainder)) print(paste("Integer Division:", int_div))
L
suffix are stored as numeric (double).typeof()
or class()
to check the type of numbers.Write a script that takes two numbers, calculates and prints:
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!