0
0
R Programmingprogramming~15 mins

Anonymous functions in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Anonymous Functions in R
📖 Scenario: You are working with a list of numbers representing daily sales in a small shop. You want to quickly calculate the square of each sale amount without creating a separate named function.
🎯 Goal: Learn how to use anonymous functions in R to apply a quick calculation to each item in a list.
📋 What You'll Learn
Create a numeric vector called sales with the exact values: 10, 20, 30, 40, 50
Create a variable called square_sales that uses lapply() with an anonymous function to square each number in sales
Print the square_sales variable to display the squared values
💡 Why This Matters
🌍 Real World
Anonymous functions help quickly perform small tasks like calculations or filtering without cluttering your code with many named functions.
💼 Career
Data analysts and programmers often use anonymous functions to write concise and readable code for data transformations and analysis.
Progress0 / 4 steps
1
Create the sales data
Create a numeric vector called sales with these exact values: 10, 20, 30, 40, 50
R Programming
Need a hint?

Use the c() function to create a vector with the given numbers.

2
Prepare to square the sales
Create a variable called square_sales and set it to lapply() applied on sales with an anonymous function that takes one argument and returns its square.
R Programming
Need a hint?

Use function(x) x^2 inside lapply() to square each element.

3
Check the type of the result
Convert square_sales from a list to a numeric vector using unlist() and store it back in square_sales.
R Programming
Need a hint?

Use unlist() to convert the list to a vector for easier use.

4
Display the squared sales
Print the square_sales vector to show the squared sales values.
R Programming
Need a hint?

Use print(square_sales) to display the vector.