0
0
R Programmingprogramming~5 mins

Useful vector functions (length, sum, mean) in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the length() function do when applied to a vector in R?
The length() function returns the number of elements in the vector. It tells you how many items are inside.
Click to reveal answer
beginner
How does the sum() function work on a numeric vector in R?
The sum() function adds up all the numbers in the vector and gives you the total.
Click to reveal answer
beginner
What is the purpose of the mean() function in R?
The mean() function calculates the average value of the numbers in a numeric vector by adding them up and dividing by the count.
Click to reveal answer
beginner
If you have a vector c(2, 4, 6, 8), what will length(), sum(), and mean() return?
length() returns 4 because there are 4 numbers.<br>sum() returns 20 because 2+4+6+8=20.<br>mean() returns 5 because 20 divided by 4 is 5.
Click to reveal answer
intermediate
Can sum() and mean() be used on non-numeric vectors?
No, sum() and mean() only work on numeric vectors. Using them on non-numeric vectors will cause an error.
Click to reveal answer
What does length(c(1, 3, 5, 7)) return?
A4
B16
C1
DError
What is the result of sum(c(10, 20, 30))?
A20
BError
C10
D60
Which function calculates the average of numbers in a vector?
Alength()
Bmean()
Csum()
Dmax()
What happens if you use sum() on a character vector like c('a', 'b')?
AReturns the number of characters
BReturns the concatenated string
CGives an error
DReturns 0
If a vector has 5 elements, what will length() return?
A5
B0
CSum of elements
DMean of elements
Explain what the functions length(), sum(), and mean() do when used on a numeric vector in R.
Think about counting, adding, and averaging.
You got /3 concepts.
    Describe a real-life example where you might use length(), sum(), and mean() on a list of numbers.
    Imagine you have a list of test scores.
    You got /4 concepts.