Introduction to R and Basic Programming Concepts
Bioinformatics Core Facility CECAD
2026-05-21
git clone https://github.com/CECADBioinformaticsCoreFacility/Beginners_R_Course_2026.git
https://cecadbioinformaticscorefacility.github.io/Beginners_R_Course_2026/
Session 3 :: More Basic Concepts in R
if-else is used to evaluate whether a statement is TRUE or FALSETRUE, the first code block is executedFALSE, the second code block is executedAn apply function is essentially a loop, but run faster than loops and often require less code. The apply family of functions is a set of functions in R that allow you to apply a function to the rows or columns of a matrix or data frame. The main functions in the apply family are:
[,1] [,2] [,3]
[1,] 1 11 21
[2,] 2 12 22
[3,] 3 13 23
[4,] 4 14 24
[5,] 5 15 25
[6,] 6 16 26
[7,] 7 17 27
[8,] 8 18 28
[9,] 9 19 29
[10,] 10 20 30
lapply is used to apply a function to each element of a list or vector and returns a list. It is useful when you want to apply a function to each element of a list and return the results in a list format.
sapply is a simplified version of lapply. It tries to simplify the result to a vector or matrix if possible.
vapply is similar to sapply, but it requires you to specify the type of output you expect. This can help prevent unexpected results.
tapply is used to apply a function to subsets of a vector, based on a grouping factor. It is useful for performing calculations on subsets of data.