In this section, We'll dive into the world of numbers and learn how to use them effectively in R.
We'll learn about the following topics:
R, like Python, offers different kinds of numbers. We’ll primarily work with integers and floating-point numbers.
Integers:
Integers in R are whole numbers, either positive or negative. To explicitly create an integer, you need to append the letter L
to the number. For example, 5L or -5L.
If you don’t append L, numbers are treated as numeric by default, which means they are floating-point numbers even if they appear to be whole numbers.
Floating-point numbers:
Floating-point numbers, or simply numerics in R, are numbers with a decimal point or those in scientific notation. They can represent a wide range of values, including very large or very small numbers. For example: 4E2 (4 times 10 to the power of 2) or 4.56.
Here’s a table with some examples of the two main number types:
Examples | Number Type |
---|---|
1L, 5L, -7L, 1400L | Integer |
1.3, -0.8, 2e2, 4E9 | Numeric (Floating-point numbers) |
Comparison with Python:
class(1L)
class(1)
#Addition
6+3
#Subtraction
7-2
#Multiplication
2*5
#Division
9/2
#Floor Division
9%/%2
Comparison with Python:
The //
operator in Python is equivalent to the %/%
operator in R for integer (floor) division. Both operators divide two numbers and return the largest integer less than or equal to the result.
#Power
5^2
#Square Root
16^0.5
Comparison with Python:
In R, the power operator is ^, whereas in Python, the power operator is **.
#Modulo
12%%7
Comparison with Python:
In R, the modulo operator is %%
, whereas in Python, the modulo operator is %
.
#Order of Operations followed in R
4 + 20 * 20 + 8 / 2
R, like Python has a set of rules for the order in which operations are performed. Multiplication and division have higher precedence than addition and subtraction. So, in the expression 4 + 20 * 20 + 8 / 2, the multiplication is done first, resulting in 100. Then, the divisioin is done. Finally, the additions are performed from left to right.
4 + (20*20) + (8/2)
You can use parentheses to specify and change orders.
#Using parentheses to specify orders
(4 + 20) * ((20 + 8) / 2)
Operation | R Operator | Python Operator | Example in R | Example in Python |
---|---|---|---|---|
Addition | + | + | 5 + 3 → 8 | 5 + 3 → 8 |
Subtraction | - | - | 5 - 3 → 2 | 5 - 3 → 2 |
Multiplication | * | * | 5 * 3 → 15 | 5 * 3 → 15 |
Division | / | / | 5 / 3 → 1.6667 | 5 / 3 → 1.6667 |
Integer (Floor) Division | %/% | // | 5 %/% 3 → 1 | 5 // 3 → 1 |
Modulo (Remainder) | %% | % | 5 %% 3 → 2 | 5 % 3 → 2 |
Exponentiation | ^ | ** | 2 ^ 3 → 8 | 2 ** 3 → 8 |
R Data Type | Description | Python Data Type | Description |
---|---|---|---|
integer | Specifically for integer values (indicated by L suffix). |
int | Represents integer values. |
numeric | Represents numbers, both integers and floats. | float | Represents floating-point numbers. |
character | Represents strings. | str | Represents strings. |
logical | Represents Boolean values (TRUE/FALSE). | bool | Represents Boolean values (True/False). |
list | An ordered collection of items, can hold different types. | list | An ordered collection of items, can hold different types. |
data.frame | A table-like structure for storing datasets. | dict | A key-value pair collection (not directly comparable to data frames). |
matrix | A two-dimensional array of homogeneous data. | tuple | An immutable ordered collection of items. |
factor | Represents categorical data. | set | An unordered collection of unique items. |