0
0
R Programmingprogramming~10 mins

Apply family vs loops in R Programming - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the mean of each column using apply.

R Programming
result <- apply(data, [1], mean)
Drag options to blanks, or click blank then click option'
A2
B1
C3
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 2 applies the function to rows, not columns.
Using 0 or 3 causes an error or unexpected results.
2fill in blank
medium

Complete the code to calculate the sum of each element in the list using lapply.

R Programming
result <- lapply(numbers, [1])
Drag options to blanks, or click blank then click option'
Asum()
Blength
Cmean
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum() calls the function immediately instead of passing it.
Using mean or length applies the wrong function.
3fill in blank
hard

Fix the error in the for loop to correctly calculate the squares of numbers.

R Programming
squares <- numeric(length(numbers))
for (i in [1](numbers)) {
  squares[i] <- numbers[i]^2
}
Drag options to blanks, or click blank then click option'
Alength
B1:length
Cseq_along
Drange
Attempts:
3 left
💡 Hint
Common Mistakes
Using length(numbers) alone does not create a sequence.
Using range(numbers) returns min and max, not a sequence.
4fill in blank
hard

Fill both blanks to create a named vector with the mean of each column using sapply.

R Programming
result <- sapply(data, [1])
names(result) <- [2](data)
Drag options to blanks, or click blank then click option'
Amean
Bcolnames
Crownames
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum instead of mean changes the calculation.
Using rownames assigns wrong names.
5fill in blank
hard

Fill all three blanks to create a list of logical vectors indicating which elements are greater than 5 using lapply.

R Programming
result <- lapply(numbers, function(x) x [1] [2])
names(result) <- [3](numbers)
Drag options to blanks, or click blank then click option'
A>
B5
Cnames
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > changes the condition.
Using length instead of names assigns wrong names.