Bird
Raised Fist0
R Programmingprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the length() function do when applied to a vector in R?
easy
A. It calculates the sum of all elements in the vector.
B. It sorts the elements in the vector.
C. It finds the average value of the elements in the vector.
D. It returns the number of elements in the vector.

Solution

  1. Step 1: Understand the purpose of length()

    The length() function counts how many items are inside a vector.
  2. Step 2: Compare with other functions

    Unlike sum() or mean(), it does not add or average values, just counts them.
  3. Final Answer:

    It returns the number of elements in the vector. -> Option D
  4. Quick Check:

    length() = count elements [OK]
Hint: Remember length() counts items, sum() adds, mean() averages [OK]
Common Mistakes:
  • Confusing length() with sum() or mean()
  • Thinking length() returns the last element
  • Assuming length() sorts the vector
2. Which of the following is the correct syntax to calculate the mean of a vector v in R?
easy
A. mean(v)
B. mean = v
C. mean[v]
D. mean v()

Solution

  1. Step 1: Recall function call syntax in R

    Functions are called with parentheses and the vector inside, like mean(v).
  2. Step 2: Check each option

    mean(v) uses correct syntax. Options B, C, and D are invalid or incorrect syntax.
  3. Final Answer:

    mean(v) -> Option A
  4. Quick Check:

    Function call uses parentheses: mean(v) [OK]
Hint: Use parentheses to call functions with arguments in R [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Assigning mean instead of calling it
  • Omitting parentheses after function name
3. What is the output of the following R code?
v <- c(2, 4, 6, 8)
sum(v) + length(v)
medium
A. 20
B. 22
C. 24
D. 26

Solution

  1. Step 1: Calculate sum(v)

    The vector v is c(2,4,6,8). Sum is 2+4+6+8 = 20.
  2. Step 2: Calculate length(v) and add

    Length is number of elements = 4. Adding sum + length = 20 + 4 = 24.
  3. Final Answer:

    24 -> Option C
  4. Quick Check:

    sum=20 + length=4 = 24 [OK]
Hint: Add sum and length values separately, then combine [OK]
Common Mistakes:
  • Adding elements incorrectly
  • Confusing length with sum
  • Forgetting to add length to sum
4. The following R code is intended to calculate the mean of vector x. What is wrong?
x <- c(5, 10, 15)
mean = mean(x)
print(mean)
medium
A. The print() function cannot print variables.
B. The variable name 'mean' overwrites the function mean(), causing errors later.
C. The vector x is not defined correctly.
D. mean() function requires two arguments.

Solution

  1. Step 1: Analyze variable assignment

    Assigning mean = mean(x) overwrites the built-in mean() function with a number.
  2. Step 2: Understand consequences

    After this, calling mean() again will cause errors because mean is no longer a function.
  3. Final Answer:

    The variable name 'mean' overwrites the function mean(), causing errors later. -> Option B
  4. Quick Check:

    Don't name variables same as functions [OK]
Hint: Avoid naming variables same as function names [OK]
Common Mistakes:
  • Thinking print() cannot print variables
  • Believing mean() needs two arguments
  • Ignoring variable shadowing of functions
5. You have a numeric vector nums with some missing values (NA). Which R code correctly calculates the mean ignoring the missing values?
hard
A. mean(nums, na.rm = TRUE)
B. mean(nums, remove.na = TRUE)
C. sum(nums) / length(nums)
D. mean(nums, na.ignore = TRUE)

Solution

  1. Step 1: Understand NA handling in mean()

    To ignore missing values, use the argument na.rm = TRUE inside mean().
  2. Step 2: Check each option

    mean(nums, na.rm = TRUE) uses correct argument na.rm. Options B and D use incorrect argument names. sum(nums) / length(nums) does not handle NA and will return NA.
  3. Final Answer:

    mean(nums, na.rm = TRUE) -> Option A
  4. Quick Check:

    Use na.rm=TRUE to ignore NA in mean() [OK]
Hint: Use na.rm=TRUE to skip NA values in mean() [OK]
Common Mistakes:
  • Using wrong argument names like remove.na or na.ignore
  • Calculating mean manually without handling NA
  • Ignoring that NA causes mean() to return NA
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard