0
0
R Programmingprogramming~30 mins

Factor in analysis and plotting in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Factor in analysis and plotting
📖 Scenario: You are working with a small dataset of fruit preferences collected from a group of people. Each person chose their favorite fruit from a list. You want to analyze how many people chose each fruit and then create a simple bar plot to visualize the counts.
🎯 Goal: Build a program that creates a factor variable for fruit preferences, counts how many times each fruit was chosen, and then plots these counts in a bar chart.
📋 What You'll Learn
Create a vector called fruits with the exact values: "apple", "banana", "apple", "orange", "banana", "banana", "apple".
Convert the fruits vector into a factor called fruit_factor.
Use the table() function on fruit_factor to count the number of each fruit and store it in fruit_counts.
Create a bar plot of fruit_counts with the title "Fruit Preferences".
💡 Why This Matters
🌍 Real World
Analyzing survey data where answers are categories, like favorite fruits, colors, or brands.
💼 Career
Data analysts and scientists often work with categorical data and need to summarize and visualize it clearly.
Progress0 / 4 steps
1
Create the fruit vector
Create a character vector called fruits with these exact values: "apple", "banana", "apple", "orange", "banana", "banana", "apple".
R Programming
Need a hint?

Use the c() function to combine the fruit names into a vector.

2
Convert to a factor
Convert the fruits vector into a factor called fruit_factor using the factor() function.
R Programming
Need a hint?

Use fruit_factor <- factor(fruits) to convert the vector to a factor.

3
Count the factor levels
Use the table() function on fruit_factor to count how many times each fruit appears. Store the result in a variable called fruit_counts.
R Programming
Need a hint?

Use fruit_counts <- table(fruit_factor) to get counts of each fruit.

4
Plot the fruit counts
Create a bar plot of fruit_counts using the barplot() function. Add the title "Fruit Preferences" using the main argument.
R Programming
Need a hint?

Use barplot(fruit_counts, main = "Fruit Preferences") to create the plot with a title.