Page 207 - Data Science class 11
P. 207
• [1] "numeric"
class(my_name)
• [1] "character"
You can also use the functions is.numeric(), and is.character(), is.logical() to check whether a variable is numeric,
character, or logical, respectively. For instance:
is.numeric(my_age)
[1] TRUE
is.numeric(my_name)
[1] FALSE
If you want to change the type of a variable to another one, use the as.* functions, including: as.numeric(),
as.character(), as.logical(), etc.
my_age
• [1] 78
# Convert my_age to a character variable
as.character(my_age)
• "78"
Note that, the conversion of a character to a numeric will output NA (for not available). R doesn’t know how to convert
a numeric variable to a character variable.
6.3 Basic arithmetic oPerations
R can be used as a calculator.
6.3.1 Basic arithmetic operators
The basic arithmetic operators are:
• + (addition)
• - (subtraction)
• * (multiplication)
• / (division)
• ^ (exponentiation)
• %% Modulus
The R Arithmetic operators include operators like Arithmetic Addition, Subtraction, Division, Multiplication,
Exponentiation, Integer Division, and Modulus. All these R arithmetic operators are binary operators, which means
they operate on two operands. 15 ^ 3 = 3375 (It means 15 Power 3 or 103).
Exercise 6.1: Working on Arithmetic Operators in the R Studio Package
Type the command directly below in the script pane.
Don’t forget to select Run from the pane each time.
# Addition
3 + 7
# Subtraction
7 - 3
# Multiplication
3 * 7
Programming with R 205

