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)
abs(x)
: Returns the absolute value of x.
abs(-5)
abs(3)
sign(x)
: Returns the sign of x as -1, 0, or 1.
sign(-5)
round(x, digits)
: Rounds x to a specified number of decimal places.
round(3.2359, 3)
ceiling(x)
: Rounds x up to the nearest integer.
ceiling(5.2)
floor(x)
: Rounds x down to the nearest integer.
floor(5.8)
trunc(x)
: Truncates x by removing the fractional part.
trunc(5.8)
signif(x, digits)
: Rounds x to the specified number of significant digits.
signif(123.456, 3)
exp(x)
: Returns the exponential of x (e^x).
exp(1)
log(x, base)
: Computes the logarithm of x to a given base (default is e for natural log).
log(10)
log(100, base=10)
sqrt(x)
: Computes the square root of x.
sqrt(16)
sin(x)
: Sine of x (in radians).
sin(pi/2)
cos(x)
: Cosine of x (in radians).
cos(0)
tan(x)
: Tangent of x (in radians).
tan(pi/4)
asin(x)
: Arc sine of x.
asin(1)
acos(x)
: Arc cosine of x.
acos(1)
atan(x)
: Arc tangent of x.
atan(1)
sinh(x)
: Hyperbolic sine of x.
sinh(1)
cosh(x)
: Hyperbolic cosine of x.
cosh(1)
tanh(x)
: Hyperbolic tangent of x.
tanh(1)
max(x1, x2, ...)
: Returns the maximum value.
max(1, 2, 3)
min(x1, x2, ...)
: Returns the minimum value.
min(1, 2, 3)
sum(x)
: Returns the sum of all elements.
sum(1, 2, 3)
prod(x)
: Returns the product of all elements.
prod(1, 2, 3)
Now that we understand how to perform calculations in R, let's learn about variables and how to assign values to them.
A variable in R is also a container that stores a value, similar to how it works in Python. You can think of it as a labeled box, and you can store different values inside that box. The name of the variable allows you to access the value stored in it.
In R, you typically use either the assignment operator <-
or =
to assign a value to a variable.
Comparison with Python:
In Python, only =
is used for assigning a value to a variable. However, in R, both <-
or =
can be used.
x <- 10 #Assigns the value 10 to the variable x
y = 5 #This also assigns the value 5 to the variable y
x
y
In R, both x and y will be printed when their names are entered after assignment, whereas in Python, only y will be displayed unless print(x)
is explicitly used to show the value of x.
#Create a variable and assign a value
a = 25
Now if you call a in the R script, R will treat it as the number 25.
a
You can use all basic arithmetic operation using variables containing values.
a + a
a * 2
When creating variable names in R, there are specific rules you need to follow:
Names cannot start with a number. For example, 2ndValue is invalid, but value2 is valid.
There can be no spaces in the name. Instead of spaces, you can use underscores (_). For example, use my_variable instead of my variable.
Avoid using certain symbols. You cannot use special characters such as: '",<>/?|()!@#$%^&*~-+. Valid characters include letters, numbers, and the dot (.) or underscore (_).
Best practice is to use lowercase letters. Although R allows uppercase letters, it's a common convention to use lowercase for readability and consistency.
Avoid using single character names that can be confusing. For example, avoid using l (lowercase "el"), O (uppercase "oh"), or I (uppercase "eye") as single-character variable names.
Avoid using reserved words or special meanings in R. Some common words like if, else, function, and data are reserved keywords and should not be used as variable names.
#Define variables
product_price = 599.99
quantity_sold = 100
#Calculate total revenue
total_revenue = product_price * quantity_sold
#Result
total_revenue
In R, variables can be reassigned to hold different values throughout the execution of a program. This means you can change the value stored in a variable at any time. This feature is also known as dynamic typing, similar to Python, and it differs from other programming languages that are statically typed.
x <- 10 #Assigns the value 10 to x
x <- 20 #Reassigns x to hold the value 20
print(x)
[1] 20
We can also use the variables themselves when doing the reassignment. Here is an example:
a
a <- a - 10
a
In R, combined assignment operators like +=
, -=
, *=
, and /=
do not exist as they do in Python. Instead, R requires the use of the standard assignment operator (<- or =) along with the arithmetic operation to update variable values.
a *= 2
a
Error in parse(text = input): <text>:1:4: unexpected '=' 1: a *= ^ Traceback:
a <- a *2
a
In R, you can check the type of object assigned to a variable using the built-in class()
function. Common data types in R include those we will discuss later:
class(1L)
class(1.5)
class('hello')
class(list(1,2,3))
#Creating a data frame
students <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(23, 22, 24),
GPA = c(3.8, 3.6, 3.9)
)
class(students)
my_matrix <- matrix(
c(1, 2, 3, 4, 5, 6), #Data for the matrix
nrow = 2, #Number of rows
ncol = 3 #Number of columns
)
class(my_matrix)
colors <- factor(c("Red", "Blue", "Green", "Blue", "Red"))
class(colors)
Table of Common Data Types
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. |