0
0
R Programmingprogramming~15 mins

Accessing columns ($, []) in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Accessing columns ($, [])
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(prices_dollar) and print(prices_brackets).