📖 Scenario: You are working as a data analyst and need to organize some survey data into a table format for easy analysis.
🎯 Goal: Create a data frame in R with specific columns and data, then display it.
📋 What You'll Learn
Create a data frame with exact column names and values
Add a variable for the number of rows
Use the data.frame() function to create the data frame
Print the data frame to show the result
💡 Why This Matters
🌍 Real World
Data frames are used to organize and analyze data collected from surveys, experiments, or business records.
💼 Career
Knowing how to create and manipulate data frames is essential for data analysts, statisticians, and anyone working with data in R.
Progress0 / 4 steps
1
Create vectors for survey data
Create three vectors called names, ages, and cities with these exact values: names = c("Alice", "Bob", "Charlie"), ages = c(25, 30, 35), cities = c("New York", "Los Angeles", "Chicago")
R Programming
Hint
Use the c() function to create vectors with the exact values given.
2
Create a variable for number of rows
Create a variable called num_rows that stores the number of elements in the names vector using the length() function
R Programming
Hint
Use length(names) to find how many names are in the vector.
3
Create the data frame
Create a data frame called survey_data using the data.frame() function with columns Name, Age, and City using the vectors names, ages, and cities respectively
R Programming
Hint
Use data.frame(Name = names, Age = ages, City = cities) to create the data frame with correct column names.
4
Print the data frame
Use print() to display the survey_data data frame
R Programming
Hint
Use print(survey_data) to show the data frame in the console.
Practice
(1/5)
1. What is the main purpose of a data.frame in R?
easy
A. To store data in rows and columns like a spreadsheet
B. To perform mathematical calculations only
C. To create graphical plots
D. To write text files
Solution
Step 1: Understand the structure of a data frame
A data frame organizes data in rows and columns, similar to a spreadsheet.
Step 2: Identify the main use
It is mainly used to store and analyze mixed types of data together.
Final Answer:
To store data in rows and columns like a spreadsheet -> Option A
Quick Check:
Data frame = tabular data storage [OK]
Hint: Think spreadsheet style data storage [OK]
Common Mistakes:
Confusing data frames with plotting functions
Thinking data frames only do math
Assuming data frames are for text files
2. Which of the following is the correct way to create a data frame with columns age and name in R?
easy
A. data.frame(age = [25, 30], name = ["Alice", "Bob"])
B. data.frame(c(25, 30), c("Alice", "Bob"))
C. data.frame(age: c(25, 30), name: c("Alice", "Bob"))
D. data.frame(age = c(25, 30), name = c("Alice", "Bob"))
Solution
Step 1: Check syntax for named columns
In R, data.frame() uses the syntax column_name = vector to name columns.
Step 2: Identify correct vector syntax
Vectors are created with c(), so c(25, 30) and c("Alice", "Bob") are correct.
Final Answer:
data.frame(age = c(25, 30), name = c("Alice", "Bob")) -> Option D
Quick Check:
Correct syntax uses = and c() [OK]
Hint: Use = to name columns and c() for vectors [OK]
Common Mistakes:
Using colons (:) instead of =
Using brackets [] instead of c()
Omitting column names
3. What will be the output of the following R code?
The code creates a data frame with two columns: x (numbers) and y (characters).
Step 2: Understand print output format
Printing a data frame shows row numbers and columns with their values in a table format.
Final Answer:
x y
1 1 a
2 2 b -> Option A
Quick Check:
Print data frame shows rows and columns [OK]
Hint: Print shows row numbers and columns in table form [OK]
Common Mistakes:
Ignoring row numbers in output
Confusing vector print with data frame print
Expecting error when none occurs
4. Identify the error in the following R code that tries to create a data frame:
df <- data.frame(age = c(20, 25), name = c("John"))
medium
A. Missing comma between vectors
B. Incorrect use of quotes around names
C. Vectors have different lengths causing recycling warning
D. data.frame() cannot mix numeric and character vectors
Solution
Step 1: Check vector lengths
The age vector has length 2, but name vector has length 1.
Step 2: Understand recycling rule
R recycles shorter vectors to match longer ones, causing a warning when lengths differ.
Final Answer:
Vectors have different lengths causing recycling warning -> Option C
Quick Check:
Unequal vector lengths cause recycling warning [OK]
Hint: Ensure all vectors have same length in data.frame() [OK]
Common Mistakes:
Assuming data.frame() needs all vectors same type
Thinking missing commas cause error here
Confusing quotes usage
5. You want to create a data frame from two vectors: ids = c(101, 102, 103) and scores = c(88, 92, 85). Then add a new column passed which is TRUE if score ≥ 90, otherwise FALSE. Which code correctly does this?
hard
A. All of the above
B. df <- data.frame(ids, scores, passed = scores >= 90)
D. df <- data.frame(ids, scores)
df$passed <- passed >= 90
Solution
Step 1: Check the one-line data.frame creation
df <- data.frame(ids, scores, passed = scores ≥ 90) correctly names columns from variable names and computes passed as logical vector.
Step 2: Eliminate incorrect options
The two-line option using if(scores ≥ 90, TRUE, FALSE) has invalid syntax for if. The other two-line uses undefined passed in passed ≥ 90. All of the above is false.