📖 Scenario: Imagine you are a data analyst working with a small dataset of students' test scores. You want to understand the data better by calculating basic statistics like the average score and identifying students who scored above a certain threshold.
🎯 Goal: You will create a simple R script that stores student scores, sets a threshold, filters students who scored above the threshold, and prints the results. This will show how R helps with statistical tasks easily.
📋 What You'll Learn
Create a named vector called scores with exact student names and their scores
Create a variable called threshold with the value 70
Use a logical condition to filter students with scores above threshold into top_students
Print the top_students vector to show the filtered results
💡 Why This Matters
🌍 Real World
Data analysts often need to quickly filter and summarize data to find important insights, like identifying top performers.
💼 Career
Knowing how to use R for basic statistics is essential for roles in data analysis, research, and any job involving data-driven decision making.
Progress0 / 4 steps
1
DATA SETUP: Create the student scores vector
Create a named numeric vector called scores with these exact entries: Alice = 58, Bob = 75, Charlie = 62, Diana = 80, Eva = 90
R Programming
Hint
Use the c() function with named elements like Alice = 58 to create the vector.
2
CONFIGURATION: Set the score threshold
Create a variable called threshold and set it to the numeric value 70
R Programming
Hint
Simply assign the number 70 to the variable threshold.
3
CORE LOGIC: Filter students scoring above the threshold
Create a new vector called top_students that contains only the entries from scores where the score is greater than threshold
R Programming
Hint
Use logical indexing with scores > threshold inside square brackets to filter.
4
OUTPUT: Print the filtered top students
Print the top_students vector to display the students who scored above the threshold
R Programming
Hint
Use the print() function to show the top_students vector.
Practice
(1/5)
1. Why is R considered essential for statistics?
easy
A. Because it is mainly used for web development
B. Because it has built-in functions for statistical analysis
C. Because it is a hardware device
D. Because it only works with text data
Solution
Step 1: Understand R's purpose
R is designed to handle data and perform statistical calculations easily.
Step 2: Compare options
Only Because it has built-in functions for statistical analysis correctly states R's strength in statistics; others are unrelated.
Final Answer:
Because it has built-in functions for statistical analysis -> Option B
Quick Check:
R is for statistics = A [OK]
Hint: R is made for stats and data analysis [OK]
Common Mistakes:
Thinking R is for web development
Confusing R with hardware
Assuming R only handles text
2. Which of the following is the correct way to create a numeric vector in R?
easy
A. numVector = {1, 2, 3, 4}
B. vector = [1, 2, 3, 4]
C. c(1, 2, 3, 4)
D. numericVector = (1, 2, 3, 4)
Solution
Step 1: Recall R vector syntax
In R, vectors are created using the c() function to combine elements.
Step 2: Check each option
Only c(1, 2, 3, 4) uses c() correctly; others use invalid syntax for R.
Final Answer:
c(1, 2, 3, 4) -> Option C
Quick Check:
Use c() for vectors = C [OK]
Hint: Use c() to combine elements into a vector [OK]
Common Mistakes:
Using square brackets like Python
Using curly braces for vectors
Using parentheses without c()
3. What will be the output of this R code?
data <- c(5, 10, 15, 20)
mean(data)
medium
A. 10
B. Error
C. 15
D. 12.5
Solution
Step 1: Understand the data vector
The vector contains values 5, 10, 15, and 20.
Step 2: Calculate the mean
Mean = (5 + 10 + 15 + 20) / 4 = 50 / 4 = 12.5
Final Answer:
12.5 -> Option D
Quick Check:
Mean of data = 12.5 [OK]
Hint: Add numbers and divide by count for mean [OK]
Common Mistakes:
Adding incorrectly
Dividing by wrong number
Expecting median instead of mean
4. Identify the error in this R code snippet:
numbers <- c(2, 4, 6, 8)
average <- mean(numbers
print(average)
medium
A. Missing closing parenthesis in mean() function
B. Incorrect variable name 'numbers'
C. Using print() instead of cat()
D. Vector c() is not defined properly
Solution
Step 1: Check syntax of mean() call
The mean function call is missing a closing parenthesis.
Step 2: Verify other parts
Variable name and vector syntax are correct; print() is valid in R.
Final Answer:
Missing closing parenthesis in mean() function -> Option A
Quick Check:
Parenthesis error = B [OK]
Hint: Count parentheses carefully in function calls [OK]
Common Mistakes:
Ignoring missing parenthesis errors
Confusing print() with cat()
Assuming variable name is wrong
5. You have a dataset with missing values. Which R function helps you quickly find the number of missing values in a vector data_vec?
hard
A. sum(is.na(data_vec))
B. mean(data_vec)
C. length(data_vec)
D. na.omit(data_vec)
Solution
Step 1: Understand missing value detection
The function is.na() returns TRUE for missing values.
Step 2: Count missing values
Using sum() on is.na() counts how many TRUE values exist.
Step 3: Check other options
mean() calculates average, length() counts all elements, na.omit() removes missing values but does not count them.
Final Answer:
sum(is.na(data_vec)) -> Option A
Quick Check:
Count missing with sum(is.na()) = A [OK]
Hint: Use sum(is.na()) to count missing values [OK]
Common Mistakes:
Using mean() to find missing values
Using length() which counts all elements
Thinking na.omit() counts missing values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.