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, 50Create a variable called
square_sales that uses lapply() with an anonymous function to square each number in salesPrint 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