[1] "X is an Integer"
Introduction to R and Basic Programming Concepts
Bioinformatics Core Facility CECAD
2026-05-21
Session 3 :: More Basic Concepts in R
Learning Purpose
Learning Objectives
if statements to execute code when a condition is TRUE.if...else and if...else if...else to choose between multiple actions.switch() to select one option from several predefined choices.if-else is used to evaluate whether a statement is TRUE or FALSETRUE, the first code block is executedFALSE, the second code block is executedLearning Purpose
Learning Objectives
Learning Purpose
Learning Objectives
An 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.