0
0
R Programmingprogramming~10 mins

Accessing columns ($, []) in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Accessing columns ($, [])
Start with data frame
Choose column name
$ operator
Extract column as vector
[
[
Use extracted column for analysis
You start with a data frame, pick a column name, then use $ or [] operators to get that column either as a vector or a data frame.
Execution Sample
R Programming
df <- data.frame(name=c("Ann", "Bob"), age=c(25, 30))
age_vector <- df$age
age_df <- df["age"]
This code creates a data frame and extracts the 'age' column as a vector and as a one-column data frame.
Execution Table
StepActionCodeResult TypeValue
1Create data framedf <- data.frame(name=c("Ann", "Bob"), age=c(25, 30))data.framename: Ann, Bob; age: 25, 30
2Extract 'age' column with $age_vector <- df$agenumeric vector25, 30
3Extract 'age' column with ["age"]age_df <- df["age"]data.frameage: 25, 30
4Check type of age_vectorclass(age_vector)character"numeric"
5Check type of age_dfclass(age_df)character"data.frame"
💡 All columns extracted; $ returns vector, ["col"] returns data frame
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
dfNULLdata.frame with 2 rows and 2 columnssamesamesame
age_vectorNULLNULLnumeric vector c(25, 30)samesame
age_dfNULLNULLNULLdata.frame with 1 column 'age'same
Key Moments - 2 Insights
Why does df$age return a vector but df["age"] returns a data frame?
Because $ extracts the column as a vector directly (see Step 2), while ["age"] keeps the data frame structure with one column (see Step 3).
Can I use df["age"] to get a vector like df$age?
No, df["age"] always returns a data frame. To get a vector, use df[["age"]] or df$age.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the type of age_vector after Step 2?
Alist
Bdata frame
Cnumeric vector
Dcharacter vector
💡 Hint
Check the 'Result Type' column in Step 2 of the execution table.
At which step does age_df get assigned a data frame?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'Result Type' columns in the execution table.
If you want to extract the 'age' column as a vector using brackets, which code would you use?
Adf[["age"]]
Bdf["age"]
Cdf$age
Ddf[age]
💡 Hint
Recall that df["age"] returns a data frame, but df[["age"]] returns a vector.
Concept Snapshot
Access columns in R data frames:
- Use $ to get a column as a vector: df$col
- Use [["col"]] to get a column as a vector: df[["col"]]
- Use ["col"] to get a one-column data frame: df["col"]
- $ and [[ ]] extract vectors; [ ] extracts data frames
- Choose based on whether you want a vector or data frame
Full Transcript
This lesson shows how to access columns in R data frames using $ and [] operators. Starting with a data frame, you can extract a column as a vector using df$col or df[["col"]]. Using df["col"] extracts the column but keeps it as a data frame with one column. The execution table traces creating a data frame, extracting the 'age' column as a vector and as a data frame, and checking their types. Key moments clarify why $ returns a vector and ["col"] returns a data frame. The quiz tests understanding of these differences. Remember, use $ or [[ ]] for vectors, and [ ] for data frames.