0
0
R Programmingprogramming~5 mins

Accessing columns ($, []) in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the $ operator do in R when working with data frames?
The $ operator extracts a column from a data frame by name, returning it as a vector.
Click to reveal answer
beginner
How do you access multiple columns from a data frame using [] in R?
Use df[, c('col1', 'col2')] to select multiple columns by name, returning a data frame.
Click to reveal answer
intermediate
What is the difference between df$col and df['col'] in R?
df$col returns the column as a vector, while df['col'] returns a data frame with one column.
Click to reveal answer
beginner
Can you use numeric indices with the [] operator to access columns in R?
Yes, df[, 2] accesses the second column of the data frame.
Click to reveal answer
intermediate
Why might you prefer df[['col']] over df$col in R?
df[['col']] allows programmatic access using a variable for the column name, while df$col requires a literal name.
Click to reveal answer
Which operator extracts a single column as a vector from a data frame in R?
A[[]]
B[]
C:
D$
What does df[, c('a', 'b')] return in R?
AA vector with columns 'a' and 'b'
BA data frame with columns 'a' and 'b'
CAn error
DA list of columns 'a' and 'b'
How do you access the third column of a data frame df by position?
Adf[, 3]
Bdf$3
Cdf[3]
Ddf[['3']]
Which method allows using a variable to select a column in R?
Adf$var
Bdf[var]
Cdf[[var]]
Ddf[, var]
What is the result type of df['col'] in R?
AData frame
BVector
CList
DMatrix
Explain how to access a single column and multiple columns from a data frame in R using $ and [].
Think about how you pick one item vs several items from a list.
You got /3 concepts.
    Describe the difference between df$col, df['col'], and df[[col]] in R.
    Consider how each method handles the column name and output type.
    You got /3 concepts.