QUESTION:How do I select a column from a dataframe using brackets [ ]?

Dataframe is commonly used in data analysis, and sometimes we need to select a single column of a dataframe. This can be done with []

Data

We’ll use the “palmerpenguins” packages (https://allisonhorst.github.io/palmerpenguins/) to address this question. You’ll need to install the package with install.packages(“palmerpenguins”) if you have not done so before, call library("“palmerpenguins”), and load the data with data(penguins)

#install.packages("palmerpenguins")
library(palmerpenguins)
## Warning: package 'palmerpenguins' was built under R version 4.1.2
data(penguins)

Selecting one column using []

At this point, “penguins” is already a dataframe, and we can use [] to select one single column

penguins[3]
## # A tibble: 344 x 1
##    bill_length_mm
##             <dbl>
##  1           39.1
##  2           39.5
##  3           40.3
##  4           NA  
##  5           36.7
##  6           39.3
##  7           38.9
##  8           39.2
##  9           34.1
## 10           42  
## # ... with 334 more rows
penguins[,3]
## # A tibble: 344 x 1
##    bill_length_mm
##             <dbl>
##  1           39.1
##  2           39.5
##  3           40.3
##  4           NA  
##  5           36.7
##  6           39.3
##  7           38.9
##  8           39.2
##  9           34.1
## 10           42  
## # ... with 334 more rows

[3] and [,3] gives the same output. The first one means “column #3” and the other is saying “every row in column 3”

Additional Reading

For more information on this topic, see https://dzone.com/articles/learn-r-how-extract-rows

Keywords

data.frame
[]
“,”
palmerpenguins