📖 Scenario: You are working with a list of numeric vectors representing daily sales for different stores. You want to calculate the total sales and average sales for each store using R.
🎯 Goal: Build a small R script that uses lapply and sapply to calculate total and average sales for each store from a list of sales data.
📋 What You'll Learn
Create a list called store_sales with three numeric vectors named store1, store2, and store3 with exact values
Create a variable called sales_threshold set to 100
Use lapply to calculate total sales for each store
Use sapply to calculate average sales for each store
Print the total sales list and average sales vector
💡 Why This Matters
🌍 Real World
Analyzing sales data from multiple stores to quickly calculate totals and averages helps businesses make informed decisions.
💼 Career
Data analysts and R programmers often use <code>lapply</code> and <code>sapply</code> to efficiently process lists of data without writing loops.
Progress0 / 4 steps
1
Create the sales data list
Create a list called store_sales with three numeric vectors: store1 with values c(120, 130, 110), store2 with values c(90, 100, 95), and store3 with values c(150, 160, 170).
R Programming
Hint
Use the list() function to create store_sales with named vectors for each store.
2
Set the sales threshold
Create a variable called sales_threshold and set it to 100.
R Programming
Hint
Just assign the number 100 to the variable sales_threshold.
3
Calculate total and average sales
Use lapply on store_sales with the function sum to calculate total sales for each store and save it in total_sales. Then use sapply on store_sales with the function mean to calculate average sales for each store and save it in average_sales.
R Programming
Hint
Use lapply(store_sales, sum) and sapply(store_sales, mean) to get totals and averages.
4
Print the results
Print the total_sales list and the average_sales vector using two separate print() statements.
R Programming
Hint
Use print(total_sales) and print(average_sales) to show the results.
Practice
(1/5)
1. What is the main difference between lapply and sapply in R?
easy
A. lapply modifies the original list, sapply does not.
B. lapply works only on vectors, sapply works only on lists.
C. lapply always returns a list, while sapply tries to simplify the result.
D. lapply is faster than sapply in all cases.
Solution
Step 1: Understand return types of lapply and sapply
lapply always returns a list regardless of the function applied.
Step 2: Understand simplification behavior of sapply
sapply tries to simplify the output to a vector or matrix if possible, otherwise returns a list.
Final Answer:
lapply always returns a list, while sapply tries to simplify the result. -> Option C
Quick Check:
Return type difference = lapply always returns a list, while sapply tries to simplify the result. [OK]
Hint: Remember: lapply = list output, sapply = simpler output if possible [OK]
Common Mistakes:
Thinking lapply works only on vectors
Believing sapply always returns a list
Assuming lapply modifies original data
2. Which of the following is the correct syntax to apply the function sqrt to each element of a list my_list using lapply?
easy
A. lapply(my_list, sqrt)
B. lapply(my_list, sqrt())
C. lapply(sqrt, my_list)
D. lapply(my_list sqrt)
Solution
Step 1: Identify correct function call syntax
The function name should be passed without parentheses to lapply, so sqrt not sqrt().
Step 2: Confirm argument order
The first argument is the list, second is the function to apply: lapply(my_list, sqrt).
Final Answer:
lapply(my_list, sqrt) -> Option A
Quick Check:
Function without () and correct order = lapply(my_list, sqrt) [OK]
Hint: Pass function name without () as second argument in lapply [OK]
Common Mistakes:
Using parentheses after function name inside lapply
Swapping argument order
Missing comma between arguments
3. What will be the output of the following R code?
What is wrong with this code if the goal is to get a numeric vector of squared values?
medium
A. Nothing is wrong; it returns a numeric vector.
B. The anonymous function is incorrect; it should use x*x instead of x^2.
C. The list elements should be named for lapply to work.
D. Using lapply returns a list, not a numeric vector.
Solution
Step 1: Understand output type of lapply
lapply always returns a list, even if function returns single values.
Step 2: Identify how to get numeric vector output
To get a numeric vector, sapply should be used instead of lapply.
Final Answer:
Using lapply returns a list, not a numeric vector. -> Option D
Quick Check:
lapply output is list, not vector [OK]
Hint: Use sapply for vector output, lapply always returns list [OK]
Common Mistakes:
Thinking lapply returns vector
Believing function syntax is wrong
Assuming names are required for lapply
5. You have a list of numeric vectors: data_list <- list(c(1,2), c(3,4,5), c(6)). You want to create a numeric vector of the means of each vector, but if a vector is empty, return NA for that element. Which code correctly does this using lapply or sapply?
hard
A. sapply(data_list, function(x) if(length(x) == 0) NA else mean(x))
B. lapply(data_list, function(x) if(length(x) == 0) NA else mean(x))
C. sapply(data_list, mean, na.rm=TRUE)
D. lapply(data_list, mean, na.rm=TRUE)
Solution
Step 1: Handle empty vectors with conditional inside function
The function checks if length is zero, returns NA; else returns mean.
Step 2: Use sapply to get numeric vector output
Using sapply simplifies the result to a numeric vector, which is desired.
Final Answer:
sapply(data_list, function(x) if(length(x) == 0) NA else mean(x)) -> Option A
Quick Check:
Conditional function + sapply for vector output [OK]
Hint: Use sapply with if-else inside function for conditional output [OK]
Common Mistakes:
Using lapply and expecting vector output
Not handling empty vectors explicitly
Passing na.rm=TRUE without checking empty vectors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.