R Beginners Course 2026

Dr. Debasish Mukherjee, Dr. Ulrike Goebel, Dr. Ali Abdallah

Bioinformatics Core Facility CECAD

2026-05-11

Session 2 :: Basic Concepts in R

Variables

  • Variables are containers for storing data values.
  • R does not have a command for declaring a variable
  • A variable is created the moment you first assign a value to it.
  • Assignment operator <- or = can be used for assigning a value

Code
# This is a comment

name <- "John"    # This is also a comment
age <- 40
name
[1] "John"
Code
name <- "Tom"
print (name)
[1] "Tom"

Variable Names

Variable names may be short (like x and y) or descriptive (age, carname, total_volume).

 

Rules for R variable names are:

  • A variable name must start with a letter or a period.
  • It can be a combination of letters, digits, period(.) and underscore(_).
  • If it starts with a period, then the period must not be followed by a digit.
  • It is case sensitive.

Variable Names

Allowed variable names:

myvar    <- "John"
my_var   <- "John"
myvar_2. <- "John"
.myvar   <- "John"

# These assignments do not overwrite "myvar":
myVar    <- "Jenny"
MYVAR    <- "Cathy"

Forbidden variable names:

my var  <- "John"  # space
           
2myvar  <- "John"  # starts with a number
.2myvar <- "John"  # starts with a '.' plus number
my-var  <- "John"  # special character
_my_var <- "John"  # starts with a '_'
my_var% <- "John"  # special character
TRUE    <- "John"  # reserved words

Data Types

Data Type (Class) Example Verify value
Character “Hello!” x<-"Hello!" print(x) class(x)
Hello!
character
Numeric (double) 1.3, 5, 4.2

x<-1.35

print(x) class(x)


1.35
numeric

Numeric (integer) 1L, 0L, 4L

x<-35L

print(x) class(x)


35
integer

Complex 2+3i x<-2+3i print(x) class(x)
2+3i
complex
Logical TRUE / FALSE x<-TRUE print(x) class(x)
TRUE
logical

R Data Structures

Variables are assigned with R-Objects and the data types of the R-objects become the data types of the variables. There are many types of R-objects. The frequently used ones are −

# Create a vector
numbers <- c('100','200',"450","670")

# Print the vector
print(numbers)
[1] "100" "200" "450" "670"


# Create a vector.
alphabets <- c("a",'b','c',"d")

# Associate names to the vector
names(numbers) <- alphabets

# Print the named vector.
print(numbers)
    a     b     c     d 
"100" "200" "450" "670" 
# Create a vector
apple_colors <- 
  c('green','green','yellow','red','red','red','green')

# Create a factor object
factor_apple <- factor(apple_colors)

# Print the factor.
print(factor_apple)
[1] green  green  yellow red    red    red    green 
Levels: green red yellow


# Create a factor object with defined levels
factor_apple <- 
  factor(apple_colors, 
         levels = c("red","yellow","green"))

# Print the factor.
print(factor_apple)
[1] green  green  yellow red    red    red    green 
Levels: red yellow green
# Create a matrix.
M = matrix( c(2,10,-4,0,7,5.5), 
            nrow = 2, 
            ncol = 3, 
            byrow = TRUE,
            dimnames = 
              list(Sex=c("F","M"),Day=1:3))

# Print the matrix
print(M)
   Day
Sex 1  2    3
  F 2 10 -4.0
  M 0  7  5.5

# Create an array with 3 dimensions:
a <- array(dim = c(2,3,2))

# Assume that Dimension 3 (=z) relates to 
# two experiments in different conditions,
a[,,1] <- M     # condition 1 
a[,,2] <- M + 2 # condition 2 

# Print the array
print(a)
, , 1

     [,1] [,2] [,3]
[1,]    2   10 -4.0
[2,]    0    7  5.5

, , 2

     [,1] [,2] [,3]
[1,]    4   12 -2.0
[2,]    2    9  7.5

Lists are vectors of a special kind. Think of their elements as “hooks”:

Each hook carries an R object:

The “prey” carried by a “hook” may be of arbitrary data type.

Specifically, a list element may "hook" another list:

This feature allows to construct complex objects, tailored to specific needs, and store them in variables for re-use.

L <- list(3, "a", c(7,-1,10), list("I am a sub-list", function(x) print(x)))
names(L) <- c("A","B","C","D")
class(L)
[1] "list"
class(L[1])
[1] "list"
L[[3]] #  Double brackets extract the value at a given position
[1]  7 -1 10
L[3] # Single brackets extract the entire 3rd list element: a list of length 1, containing the value
$C
[1]  7 -1 10
# Create the data frame
BMI <-  data.frame(
   gender = c("Male", "Male","Female"), 
   height = c(152, 171.5, 165), 
   weight = c(81,93, 78),
   Age = c(42,38,26)
)

# Print the data frame
print(BMI)
  gender height weight Age
1   Male  152.0     81  42
2   Male  171.5     93  38
3 Female  165.0     78  26

Operators

Operators are the symbols that tell the compiler to perform specific mathematical or logical manipulations. R language is rich in built-in operators and provides the following types of operators −

Operator Name Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
^ Exponent x ^ y
%% Modulus (Remainder from division) x %% y
%/% Integer Division x%/%y
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Operator Description
& Element-wise Logical AND operator. It returns TRUE if both elements are TRUE
&& Logical AND operator - Returns TRUE if both statements are TRUE
| Elementwise- Logical OR operator. It returns TRUE if one of the statement is TRUE
|| Logical OR operator. It returns TRUE if one of the statement is TRUE.
! Logical NOT - returns FALSE if statement is TRUE
my_var <- 3

3 -> my_var

assign("my_var", c(10.4, 5.6, 3.1, 6.4, 21.7))

my_var # print my_var
[1] 10.4  5.6  3.1  6.4 21.7
Operator Description Example
: Creates a series of numbers in a sequence x <- 1:10
%in% Find out if an element belongs to a vector x %in% y
%*% Matrix Multiplication x <- Matrix1 %*% Matrix2

“Everything in R is a Vector”: Compact Notations

Unlike many other programming languages, R does not distinguish between an item of a data type (e.g., a single numeric value) and a vector of length one of the same type
Almost all R operators and many R functions are vectorized, that is

  • their arguments can be vectors of any length
  • they apply themselves to each position in the input vector(s) in turn, and
  • they return a vector of the per-position results

This allows for very compact notation:

vsum <- 1:100 + 1:100

head(vsum)
[1]  2  4  6  8 10 12
tail(vsum)
[1] 190 192 194 196 198 200

“Everything in R is a Vector”: Recycling

If the input vectors of a vectorized operation are not equal in length, then a behavior kicks in which can cause bugs which are hard to find: shorter input vectors are simply repeated until the length of the longest vector is reached.

v1 <- c("A","B","C","D","E")
v2 <- c("u","v")
v3 <- "X"

paste(v1,v2,v3,sep="|")
[1] "A|u|X" "B|v|X" "C|u|X" "D|v|X" "E|u|X"

 

The example shows that a vector of length 1 is paired with every element of the output. This can be exploited to add or multiply a single number to/with all elements of a vector:

v <- 1:3

v + 2
[1] 3 4 5
v * 3
[1] 3 6 9