2  Basics of R

2.1 Using R as a calculator

You may use R as a calculator. Some examples are given below.

# Addition
5 + 4
[1] 9
# Subtraction
5 - 4
[1] 1
# Multiplication
3 * 6
[1] 18
# Division
10 / 2
[1] 5
# Exponents
2^3
[1] 8
# Modulo
5 %% 2
[1] 1

2.1.1 Basic Operators

Operator Description
Arithmetic
+ Addition
- Subtraction
* Multiplication
/ Division
^ or ** Exponential
%% Modulus
% / % Integer Division
Logic
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Exactly equal to
!= Not equal to
!x Not x
x | y x OR y
x & y x AND y

2.1.2 Order of operators

  • Parenthesis

  • Multiplication / division

  • Addition / subtraction

  • Multiplication has the same importance as division. Similarly, addition and subtraction are at the same level. When we need to decide between the two, we apply the operation that shows first from the left to the right.

  • Use of parentheses makes it easier to perform the correct operation

  • Can you guess the result of the following operation?

    • 8 / 2 * ( 2 + 2)
8 / 2 * ( 2 + 2)
[1] 16
8 / 2 * 2 + 2
[1] 10
100 * 2 + 50 / 2
[1] 225
(100 * 2) + (50 / 2)
[1] 225

2.2 Storing information in objects

R lets you save data by storing it inside an R object. An object is a name that you can use to call up stored data.

a <- 5

a
[1] 5
a + 2
[1] 7

In the example above, we store value of 5 under object a. We then call the value stored under a and sum it with 2.

Note the use of < together with - . This representation (<-) resembles a backward pointing arrow, and it assigns the value 2 to the object a.

b_vector <- 1:6
b_vector
[1] 1 2 3 4 5 6
## [1] 1 2 3 4 5 6

In the above example, we create a vector, whose elements are numbers from 1 to 6 and store it under b_vector.

When you create an object, the object will appear in the environment pane of RStudio (on the top right-hand-side of the R screen). This pane will show you all of the objects you’ve created since opening RStudio.

2.2.1 Naming of objects

Note the following;

  • An object name cannot start with a number (for example, 2var or 2_var)

  • A name cannot use some special symbols, like ^, !, $, @, +, -, /, or * . You may use _

  • R is case-sensitive, so name and Name will refer to different objects

  • R will overwrite any previous information stored in an object without asking your confirmation. So, be careful while making changes.

  • You can see which object names you have already used by calling the function ls:

    ls()
    [1] "a"        "b_vector"
    ## [1] "a"        "b_vector"

2.2.2 Naming conventions

You may see the following styles for naming of variables:

  • Camel case

Camel case variable naming is common in Javascipt. However, it is considered as bad practise in R. Try to avoid this kind of naming.

bankAccount = 100
  • Use of dots

dot is used in variable names by many R users. However, try to avoid this too because base R uses dots in function names (contrib.url()) and class names (data.frame). Avoiding dot in your variable names will help you avoid confusion, particularly in the initial stages of your learning!

bank.account = 100
  • Snake case

Use of snake case is considered to be good practice. Try to follow this approach.

bank_account = 100

Note that you may find different users of R having a preference towards different styles. The recommendations above are from the “Tidyverse style guide”, which is available from https://style.tidyverse.org.

Start your variable names with a lower case and reserve the capital letter start for function names!

2.2.3 Removing objects

You will see that the Environment window can quickly get over-crowded while working interactively. You may remove the objects that you no longer need. by rm(object_name )

rm(a)

2.2.4 Example of using variables

Let us calculate the module mark for a student who got 65% from coursework and 53% from exam. The weights for the coursework and exam are, respectively, 25% and 75%.

# let's calculate module mark for a student
coursework <- 65
exam <- 53
module_mark <- coursework * 0.25 + exam * 0.75

print(module_mark)
[1] 56

2.3 Datatypes in R

2.3.1 Numeric

Decimal numbers and integers are part of the numeric class in R.

2.3.1.1 Decimal (floating point values)

decimal_number <- 2.2

2.3.1.2 Integer

i <- 5

2.3.2 Logical

Boolean values (TRUE and FALSE) are part of the logical class in R. These are written in capital letters.

t <- TRUE
f <- FALSE
t
[1] TRUE
f
[1] FALSE

2.3.3 Characters

Text (string) values are known as characters in R. You may use single or double quotation to create a text (string).

message <- "hello all!"
print(message)
[1] "hello all!"
an_other_message <- 'how are you?'
print(an_other_message)
[1] "how are you?"

2.3.4 Checking data type classes

We can use the class() function to check the data type of a variable:

class(decimal_number)
[1] "numeric"
class(i)
[1] "numeric"
class(t)
[1] "logical"
class(f)
[1] "logical"
class(message)
[1] "character"