📖 Scenario: You have a small table of fruits and their prices. You want to get the prices using two common ways in R: the $ operator and the [] operator.
🎯 Goal: Learn how to access columns in a data frame using $ and [] in R.
📋 What You'll Learn
Create a data frame called fruits with columns name and price
Create a variable called col_name with the value "price"
Use the $ operator to get the price column and save it in prices_dollar
Use the [] operator with col_name to get the price column and save it in prices_brackets
Print both prices_dollar and prices_brackets
💡 Why This Matters
🌍 Real World
Accessing columns in data frames is a common task when working with data in R, such as analyzing sales, survey results, or scientific data.
💼 Career
Data analysts and scientists frequently use these techniques to manipulate and explore data sets efficiently.
Progress0 / 4 steps
1
Create the data frame
Create a data frame called fruits with two columns: name containing "apple", "banana", "cherry" and price containing 1.2, 0.5, 2.5.
R Programming
Hint
Use data.frame() with name = c(...) and price = c(...).
2
Create the column name variable
Create a variable called col_name and set it to the string "price".
R Programming
Hint
Use <- "price" to create the variable.
3
Access the price column using $ and []
Use the $ operator on fruits to get the price column and save it in prices_dollar. Then use the [] operator with col_name on fruits to get the same column and save it in prices_brackets.
R Programming
Hint
Use fruits$price and fruits[[col_name]].
4
Print the results
Print the variables prices_dollar and prices_brackets to show the prices.
R Programming
Hint
Use print(prices_dollar) and print(prices_brackets).
Practice
(1/5)
1. In R, which operator is used to extract a column from a dataframe as a vector by its name?
easy
A. The $ operator
B. The [] operator with column name
C. The [[]] operator with column index
D. The : operator
Solution
Step 1: Understand $ operator usage
The $ operator extracts a column from a dataframe as a vector by its name.
Step 2: Compare with other operators
The [] operator returns a dataframe, [[]] returns a vector but requires double brackets, and : is unrelated.
Final Answer:
The $ operator -> Option A
Quick Check:
$ extracts vector column = C [OK]
Hint: Use $ for quick vector column access by name [OK]
Common Mistakes:
Using [] returns a dataframe, not a vector
Confusing [[]] with []
Using : operator for columns
2. Which of the following is the correct syntax to get a one-column dataframe named 'age' from dataframe df?
easy
A. df['age']
B. df$age
C. df[['age']]
D. df.age
Solution
Step 1: Identify syntax for one-column dataframe
Using single brackets with a column name, like df['age'], returns a one-column dataframe.
Step 2: Differentiate from other options
df$age and df[['age']] return vectors, df.age is invalid syntax in R.
Final Answer:
df['age'] -> Option A
Quick Check:
Single brackets with name = one-column dataframe [OK]
Hint: Use single brackets with quotes for one-column dataframe [OK]
Common Mistakes:
Using $ returns a vector, not dataframe
Using [[]] returns a vector, not dataframe
Using dot notation (df.age) which is invalid
3. Given the dataframe df <- data.frame(name=c('Ann','Bob'), age=c(25,30)), what is the output of class(df['age'])?
medium
A. "integer"
B. "numeric"
C. "data.frame"
D. "vector"
Solution
Step 1: Understand df['age'] output type
Using single brackets with a column name returns a dataframe, so class(df['age']) is "data.frame".
Step 2: Differentiate from vector classes
Using df$age or df[['age']] returns a vector of class "numeric" or "integer" but not a dataframe.
4. Consider the code: df <- data.frame(x=1:3, y=4:6); col <- df['x'] + 1. What is the error in this code?
medium
A. The column name 'x' is incorrect
B. You cannot add 1 to a dataframe
C. df['x'] returns a vector, so addition is valid
D. The dataframe df is not defined
Solution
Step 1: Analyze df['x'] type
df['x'] returns a one-column dataframe, not a vector.
Step 2: Understand addition operation
Adding 1 to a dataframe causes an error because arithmetic operations expect vectors or numbers.
Final Answer:
You cannot add 1 to a dataframe -> Option B
Quick Check:
Adding number to dataframe causes error = A [OK]
Hint: Use [[]] or $ to get vector before arithmetic [OK]
Common Mistakes:
Assuming df['x'] is a vector
Using wrong column name
Ignoring that df is defined
5. You want to add 5 to every value in the 'score' column of dataframe df and keep the result as a vector. Which code correctly does this?
hard
A. df.score + 5
B. df['score'] + 5
C. df[score] + 5
D. df[['score']] + 5
Solution
Step 1: Identify how to get vector column
df[['score']] extracts the 'score' column as a vector suitable for arithmetic.
Step 2: Check other options
df.score is invalid syntax in R (Python-style dot notation), df['score'] returns a dataframe causing error on addition, df[score] is invalid without quotes around column name.
Final Answer:
df[['score']] + 5 -> Option D
Quick Check:
Double brackets extract vector for math = D [OK]
Hint: Use double brackets to get vector for calculations [OK]
Common Mistakes:
Using single brackets returns dataframe, causing errors
Forgetting quotes around column name in []
Assuming df[score] works without quotes
Using Python-style dot notation (df.score)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.