Select single column in a dataframe by position


Introduction

We have covered basics of subsetting tibble here. If we only want to select single column of tibble for further processing using position we use the [[]] operator.

Procedure

We will be working with the following tibble:

 
# package for creating tibble
library(tibble)

# create tibble
basic_tibble <- tibble(
  Name = c("Anthony Stark", "Steve Rogers","Bruce Banner","Carol Denvers"),
  Age = c(45,115,47,80),
  `Alias Name` = c("Ironman","Captain America","Hulk","Captain Marvel")
  )

The tibble contains the following data:

basic tibble

We will select the 2nd column.

Code

 
# refer procedure for definition of basic_tibble
View(basic_tibble[[2]])

The output of above code is:

position

Conclusion

Thus we have successfully covered selecting single column in a dataframe by position.

References

  • https://r4ds.had.co.nz/