0
0
R Programmingprogramming~10 mins

lapply and sapply in R Programming - Interactive Code Practice

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

Complete the code to apply the function to each element of the list using lapply.

R Programming
result <- lapply(my_list, [1])
Drag options to blanks, or click blank then click option'
Alist
Bsum
Cc
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function that does not accept a vector input.
Using a function that returns a list instead of a numeric value.
2fill in blank
medium

Complete the code to apply the function to each element of the list and simplify the result using sapply.

R Programming
result <- sapply(my_list, [1])
Drag options to blanks, or click blank then click option'
Alength
Blist
Csum
Dc
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function that returns a list, which prevents simplification.
Using a function that returns the length, which is correct but not the intended answer here.
3fill in blank
hard

Fix the error in the code to correctly apply the mean function to each element using lapply.

R Programming
result <- lapply(my_list, [1])
Drag options to blanks, or click blank then click option'
Amean
Bsum
Clength
Dmedian
Attempts:
3 left
💡 Hint
Common Mistakes
Including parentheses after the function name inside lapply.
Using a function that does not calculate the mean.
4fill in blank
hard

Fill both blanks to create a named vector of the lengths of each element in the list using sapply.

R Programming
lengths <- sapply([1], [2])
Drag options to blanks, or click blank then click option'
Amy_list
Blength
Csum
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum instead of length for counting elements.
Using a variable name that does not exist.
5fill in blank
hard

Fill both blanks to create a list of squared values for each element in the list using lapply.

R Programming
squares <- lapply(my_list, function(x) x[1][2])
Drag options to blanks, or click blank then click option'
A*
B2
C^
D**
Attempts:
3 left
💡 Hint
Common Mistakes
Using the multiplication operator '*' instead of the power operator '^'.
Using the wrong exponent.