R Basics I - Operators, Logic & Data Types

Data Science with R
Author

John Robin Inston

Published

March 8, 2025

Modified

May 30, 2025

This post contains notes for Chapter 2 of my course series Data Science with R covering the basic use of R as a calculator including computations, logic, data types and the assignment operator.

Calculations

We can perform a wide variety of mathematical calculations using R including:

  • Multiplication, addition, division, subtraction;
  • Raising to powers and square rooting;
  • Trigonometric functions;
  • Exponential and logarithmic functions.

The syntax to apply these operations is often very intuitive but slightly different to the mathematical notation. Note that all computations written in R follow the BIDMAS (Brackets, Indices, Division, Multiplication, Addition, Subtraction) order of operations rule and so take care to ensure clarity with parentheses if necessary.

Addition, Subtraction, Multiplication and Division

# Addition
21 + 47 
[1] 68
# Subtraction
456 - 564 
[1] -108
# Multiplication
7*23      
[1] 161
# Division
45/23     
[1] 1.956522

Powers and Square Roots

To raise numbers to powers we can use the ^ symbol. Note that if we wish to raise numbers to fractional powers we must ensure to enclose our fraction in parentheses (). Further, there is a specifically defined function for computing the square root of a number sqrt().

Important

Parentheses (), brackets [] and braces {} all have different uses in R and care should be taken to apply them correctly.

# Powers 
9^2
[1] 81
# Fractional Powers
225^(1/2)
[1] 15
# Square Root Function
sqrt(225)
[1] 15

Logarithms and Exponentials

As a quick reminder in mathematics we define the irrational number \(e\) by \[ e = 2.71828... \]

The exponential function \(\exp(\cdot)\) is defined as \[ \exp(x) = e^x. \]

The inverse of this function is known as the natural logarithm \(\log(\cdot)=\ln(\cdot)\) and is defined as the power of which \(e\) will have to be raised to in order to give \(x\). It is therefore the inverse of the exponential function and satisfies \[ \exp(\log(x)) = e^{\log(x)} = x. \]

For a more indepth look at these functions and the results we use to perform calculations with them see my note on Log Laws. To apply these functions in R we use the exp() and log() functions:

# Exponential function
exp(1)
[1] 2.718282
exp(17)
[1] 24154953
# Log function
log(47)
[1] 3.850148
# Inverse example
log(exp(47))
[1] 47
Exercise 1

Verify the following mathematical computations in R using the operations above:

  1. \(\left(\frac{\log(49^2)}{\exp(4)}\right)^{1/3}=0.5223981\);

  2. \(\left(67+\left(\frac{12}{5} \times \frac{3}{12}\right)\right)^e+\left(\log(21) - \sqrt{14}\right)=94255.7\)

Logic

R can also handle logical inputs including:

  • < less than;
  • <= less than or equal to;
  • > greater than;
  • >= greater than or equal to;
  • == equal to;
  • != not equal to;
  • & AND (\(\cap\) written mathematically);
  • | OR (\(\cup\) written mathematically).

We can write a logical expression and the return will note whether the statement was true or false. For example, the output below is FALSE because the statement “4 is greater than or equal to 5” is false:

# Logical example
4 >= 5
[1] FALSE

Note that to combine logical expressions we must use & or |! For example, trying to evaluate 2 < 4 < 5 will result in an error message.

Data Types

If we were to save the output of the previous logical expression to an object b we would see in our environment tab that b is a boolean type. R has the capacity to handle multiple data types including:

  1. Numeric - split into two sub-categories:
    1. Integer - integer values i.e. whole numbers;
    2. Double - decimal values (default over integer);
  2. Boolean / Logical - TRUE / FALSE output of some logical query;
  3. Character - Any number or character enclosed in '' or "".

A sequential collection of characters forms a string e.g. "datascience" but note that this is not a specific datatype in R unlike other languages such as python.

Exercise 2

What will the following logical queries output:

  1. 5 > 7
  2. 10 <= 10
  3. 13 != 12
  4. "Hello" >= "Hell"
  5. "Hello" != "Hell" | 5 < 4
  6. 3 < 5 < 7

Assignment Operator

The assignment operator -> is used to define objects in R, i.e. to store them in our short-term memory to use multiple times. Objects can take many different forms from basic numbers to vectors, matrices or even functions, all of which we detail in Chapter 3. For now, suppose we wish to store the results of some of the above computations for future use. We can do so by applying the assignment oeprator:

# Define object a
a <- (log(49^2)/exp(4))^(1/3)

Running this code you should see the object a appear in your environment tab and you might have noticed no result was printed in the console. To print the value of a in your console you can either use the print() function or just call the object in your console:

# Print function
print(a)
[1] 0.5223981
# Call Object
a
[1] 0.5223981

← Previous Chapter

Next Chapter →

Back to top