We use $ and [] to get specific columns from a table of data. This helps us look at or change just the part we want.
Accessing columns ($, []) in R Programming
dataframe$column_name dataframe["column_name"] dataframe[["column_name"]]
The $ sign is used to pick a column by name directly.
The [] brackets can select columns by name or position, but using double brackets [[ ]] returns the column as a vector.
df$age
df["age"]df[["age"]]df[2]This program creates a small table with names and ages. It shows three ways to get the 'age' column: with $, with single [], and with double [[]]. It prints each result so you can see the difference.
df <- data.frame(name = c("Anna", "Ben", "Cara"), age = c(25, 30, 22)) # Access age column using $ age_vector <- df$age print(age_vector) # Access age column using single [] age_df <- df["age"] print(age_df) # Access age column using double [[]] age_vector2 <- df[["age"]] print(age_vector2)
Using $ is simple but only works with column names that are valid variable names (no spaces or special characters).
Using [] with column names returns a dataframe, which keeps the column as a table.
Using [[]] returns the column as a simple vector, which is useful for calculations.
Use $ to quickly get a column by name as a vector.
Use [] with a column name to get a one-column dataframe.
Use [[]] to get a column as a vector when you want to work with its values directly.
Practice
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 AQuick Check:
$ extracts vector column = C [OK]
- Using [] returns a dataframe, not a vector
- Confusing [[]] with []
- Using : operator for columns
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 AQuick Check:
Single brackets with name = one-column dataframe [OK]
- Using $ returns a vector, not dataframe
- Using [[]] returns a vector, not dataframe
- Using dot notation (df.age) which is invalid
df <- data.frame(name=c('Ann','Bob'), age=c(25,30)), what is the output of class(df['age'])?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.Final Answer:
"data.frame" -> Option CQuick Check:
Single bracket returns dataframe class = A [OK]
- Assuming df['age'] returns a vector
- Confusing class of vector and dataframe
- Using df$age which returns numeric vector
df <- data.frame(x=1:3, y=4:6); col <- df['x'] + 1. What is the error in this code?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 BQuick Check:
Adding number to dataframe causes error = A [OK]
- Assuming df['x'] is a vector
- Using wrong column name
- Ignoring that df is defined
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 DQuick Check:
Double brackets extract vector for math = D [OK]
- 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)
Ready to prove it?
Pass the test and this pattern moves unknown
