0
0
R Programmingprogramming~10 mins

Anonymous functions 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 create an anonymous function that adds 5 to its input.

R Programming
add_five <- function(x) [1] x + 5
add_five(10)
Drag options to blanks, or click blank then click option'
Afunction(x)
Bfunction()
Cfunction(y)
Dfunction(x, y)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function without parameters like function()
Using a different parameter name than x
2fill in blank
medium

Complete the code to apply an anonymous function that squares each element in the vector.

R Programming
squared <- sapply(c(1, 2, 3), [1](x) x^2)
squared
Drag options to blanks, or click blank then click option'
Afunction()
Bfunction(x)
Cfunction(y)
Dfunction(z)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function without parameters
Using a parameter name not matching the function argument
3fill in blank
hard

Fix the error in the anonymous function that multiplies input by 3.

R Programming
triple <- [1](x) x * 3
triple(4)
Drag options to blanks, or click blank then click option'
Afunction x
Bfunc(x)
Cfunction(x)
Dfun(x)
Attempts:
3 left
💡 Hint
Common Mistakes
Missing parentheses after function keyword
Using incorrect keywords like func or fun
4fill in blank
hard

Fill both blanks to create an anonymous function that returns TRUE if input is greater than 10.

R Programming
check_gt_10 <- [1](x) x [2] 10
check_gt_10(15)
Drag options to blanks, or click blank then click option'
Afunction
Bif
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using if instead of function to define the function
Using the wrong comparison operator
5fill in blank
hard

Fill all three blanks to create a list of squares for numbers greater than 3 using an anonymous function.

R Programming
result <- lapply(1:5, [1](num) if (num [2] 3) [3] num^2 else NULL)
result
Drag options to blanks, or click blank then click option'
Afunction
B>
Creturn
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using if instead of function to define the function
Using < instead of > for comparison
Omitting the return keyword