Basic data types


R works with several data types and data structures, including:

  • Scalars
  • Vectors (numeric, character, logical)
  • Matrices
  • Data frames
  • Lists

Basic types

  • 4.5 is a decimal number (type: numeric).
  • 4L is an integer (type: integer). Integers are a subset of numeric values.
  • TRUE or FALSE are Boolean values (type: logical).
  • Values inside " " or ' ' are text (strings). They are stored as type: character.

You can check the type of an object with class().

Example 1:

# Numeric
x <- 28
class(x)
## [1] "numeric"

Example 2:

# Character
y <- "R is fantastic"
class(y)
## [1] "character"

Example 3:

# Logical
z <- TRUE
class(z)
## [1] "logical"

Example 4 (optional):

# Integer (note the L suffix)
w <- 4L
class(w)
## [1] "integer"

Variables


Variables store values and are a key component in programming. In R, a variable can store almost anything: a number, an object, a statistical result, a vector, a dataset, a model, or predictions. You can reuse a variable later by typing its name.

To create a variable, you assign a value to a name. Variable names should not contain spaces; you can use _ to separate words.

To assign a value, use <- (recommended) or =.

Here is the syntax:

# Recommended assignment operator
name_of_variable <- value

# Also used sometimes (e.g., inside function calls)
name_of_variable = value

In the console, you can run the following examples:

Example 1:

# Create a variable
x <- 42
x
## [1] 42

Example 2:

y <- 10
y
## [1] 10

Example 3:

# Use variables in an expression
x - y
## [1] 32

Vectors


A vector is a one-dimensional sequence of values. You can create a vector from any basic type. The simplest way to build a vector is using c().

Example 1:

# Numeric
vec_num <- c(1, 10, 49)
vec_num
## [1]  1 10 49

Example 2:

# Character
vec_chr <- c("a", "b", "c")
vec_chr
## [1] "a" "b" "c"

Example 3:

# Logical
vec_lgl <- c(TRUE, FALSE, TRUE)
vec_lgl
## [1]  TRUE FALSE  TRUE

Example 4:

You can perform arithmetic operations on vectors element-by-element.

vect_1 <- c(1, 3, 5)
vect_2 <- c(2, 4, 6)

sum_vect <- vect_1 + vect_2
sum_vect
## [1]  3  7 11

Example 5:

In R, you can slice a vector using square brackets []. For example, x[1:5] selects elements 1 to 5.

slice_vector <- 1:10
slice_vector[1:5]
## [1] 1 2 3 4 5

Example 6:

A quick way to create a sequence is :.

1:10
##  [1]  1  2  3  4  5  6  7  8  9 10

Arithmetic Operators


Let’s look at basic arithmetic operators in R:

Operator Description
Addition
Subtraction
Multiplication
/ Division
^ Exponentiation
%% Modulo (remainder)

Notes:

  • Lines starting with # are comments; R ignores them.
  • In rendered R Markdown, printed output is often shown with a ## prefix.

Example 1:

# Addition
3 + 4
## [1] 7

Example 2:

# Multiplication
3 * 5
## [1] 15

Example 3:

# Division
(5 + 5) / 2
## [1] 5

Example 4:

# Exponentiation
2^5
## [1] 32

Example 5:

# Modulo
28%% 6
## [1] 4

Logical Operators


Logical operators return TRUE or FALSE. They are used to test conditions and filter values.

Operator Description
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Exactly equal to
!= Not equal to
! Not x
x & y x AND y (vectorised)
x &#124; y x OR y (vectorised)
isTRUE(x) Test whether x is TRUE

To select values that satisfy a condition, use:

variable_name[condition]

Example 1:

# Create a vector from 1 to 10
logical_vector <- 1:10

# Test a condition (returns TRUE/FALSE for each element)
logical_vector > 5
##  [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE

In the output above, R compares each element to the condition logical_vector > 5. If the element is strictly greater than 5, the result is TRUE; otherwise it is FALSE.

Example 2:

To extract only the values that meet the condition, put the condition inside [] after the vector.

logical_vector[logical_vector > 5]
## [1]  6  7  8  9 10

Example 3:

You can combine conditions with & (AND) and use parentheses for clarity.

logical_vector[(logical_vector > 4) & (logical_vector < 7)]
## [1] 5 6
 

A work by Gianluca Sottile

gianluca.sottile@unipa.it