Loading a Module#
If the package has not been submitted to the standard R repositories, you cannot use the library function. You instead can call the module system on the server and load the software (if installed) manually using the module() function.
# Load a module
module("load prokka")
Note: the packages will be loaded permanently. If you want to unload a module you need to manually purge it
# Unload a module
module("purge prokka")
Apply() and tapply()#
The apply() and tapply() functions are used to apply a certain function to a data frame, list, or vector and return the result as a list or a vector depending on the function we use.
The apply() function#
The apply function is used to apply a certain function to the rows or columns of a matrix, data frame or array.
Syntax: apply(x, margin, function)
Parameters:
x: determines the input matrix, data frame, or array.
margin: 1 for rows, 2 for columns.
function: defines the function that is to be applied to each row (if margin = 1) or column (if margin = 2).
Let’s say we have a matrix mat and we want to calculate the sum of each column:
# Create the matrix
mat <- matrix(1:12, nrow = 4, ncol = 3)
# Apply the function
result <- apply(mat, 2, sum)
# Show results
print(result)
The tapply() function#
The tapply function is used to apply a certain function to subsets of a vector or array based on a factor. It’s particularly useful for performing operations on data grouped by categories.
Syntax: tapply(x, index, function)
Parameters:
x: determines the input vector or an object.
index: a vector containing categories.
function: defines the function that is to be applied to each category.
Let’s say we have a vector of test scores and a factor indicating which class each score belongs to, and we want to calculate the mean score for each class:
# Create the data
scores <- c(85, 92, 78, 88, 90, 75, 82, 95, 89)
classes <- factor(c("A", "B", "A", "C", "B", "C", "A", "B", "C"))
# Apply the function
result <- tapply(scores, classes, mean)
# Show the results
print(result)