0
0
R Programmingprogramming~5 mins

Apply functions on matrices in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What function in R is commonly used to apply a function to the rows or columns of a matrix?
The apply() function is used to apply a function to the rows or columns of a matrix in R.
Click to reveal answer
beginner
How do you specify whether to apply a function to rows or columns in the apply() function?
You use the MARGIN argument: 1 for rows and 2 for columns.
Click to reveal answer
beginner
Example: What does apply(mat, 1, sum) do if mat is a matrix?
It calculates the sum of each row in the matrix mat, returning a vector of row sums.
Click to reveal answer
beginner
Can you use apply() to find the mean of each column in a matrix? How?
Yes. Use apply(mat, 2, mean) to calculate the mean of each column.
Click to reveal answer
intermediate
What is the difference between apply() and lapply() in R?
apply() works on matrices or arrays applying a function over rows or columns, while lapply() works on lists applying a function to each element.
Click to reveal answer
In R, what does apply(mat, 2, sum) do?
ACalculates the sum of each column in mat
BCalculates the sum of each row in mat
CCalculates the sum of all elements in mat
DCalculates the mean of each column in mat
Which argument in apply() specifies whether to apply a function to rows or columns?
AFUN
BINDEX
CDATA
DMARGIN
What will apply(mat, 1, mean) return?
AMean of each row
BMean of each column
CSum of each row
DSum of each column
If you want to apply a function to each element of a list, which function is more appropriate than apply()?
Asapply()
Blapply()
Ctapply()
Dmapply()
What type of object does apply() expect as input?
AList
BData frame
CMatrix or array
DVector
Explain how to use the apply() function to calculate the sum of each row in a matrix.
Remember MARGIN = 1 means rows.
You got /5 concepts.
    Describe the difference between apply() and lapply() in R and when to use each.
    Think about the data structure you have.
    You got /5 concepts.