0
0
R Programmingprogramming~20 mins

Useful vector functions (length, sum, mean) in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vector Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:00remaining
What is the output of this R code using length()?
Consider the vector v <- c(3, 7, 2, 9, 4). What does length(v) return?
R Programming
v <- c(3, 7, 2, 9, 4)
length(v)
A4
B9
C5
DError: object 'v' not found
Attempts:
2 left
💡 Hint
The length() function returns how many elements are in the vector.
Predict Output
intermediate
1:00remaining
What is the sum of elements in this vector?
Given nums <- c(10, 20, 30, 40), what does sum(nums) return?
R Programming
nums <- c(10, 20, 30, 40)
sum(nums)
AError: object 'nums' not found
B40
C10
D100
Attempts:
2 left
💡 Hint
sum() adds all numbers in the vector.
Predict Output
advanced
1:30remaining
What is the mean of this vector with NA values?
Given data <- c(5, NA, 15, 20), what does mean(data, na.rm = TRUE) return?
R Programming
data <- c(5, NA, 15, 20)
mean(data, na.rm = TRUE)
AError: missing value and na.rm not set to TRUE
B13.33333
C15
DNA
Attempts:
2 left
💡 Hint
na.rm = TRUE tells mean() to ignore NA values.
Predict Output
advanced
1:30remaining
What error does this code produce?
What happens when you run sum(c(1, 2, '3')) in R?
R Programming
sum(c(1, 2, '3'))
A6
BError: invalid 'type' (character) of argument
C3
DNA
Attempts:
2 left
💡 Hint
sum() expects numeric values, but '3' is a character here.
🧠 Conceptual
expert
2:00remaining
How many elements are in the vector after this operation?
If v <- c(1, 2, 3, 4, 5) and then v <- v[-c(2, 4)] is run, what is length(v)?
R Programming
v <- c(1, 2, 3, 4, 5)
v <- v[-c(2, 4)]
length(v)
A3
B4
C2
D5
Attempts:
2 left
💡 Hint
Removing elements 2 and 4 means those positions are gone from the vector.