Extract or replace columns in a data frame using `$`

[This article was first published on Quantargo Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Columns in a data frame can be easily extracted and manipulated with the $ operator. Even new columns can be added by assigning a vector.

  • Extract columns from a data frame with the $.
  • Replace values of existing columns in a data frame.
  • Add new columns to a data frame.
___$___
___$___  <- ___

Extract columns with the $

___$___
___$___  <- ___

Data frames are tables resulting from the combination of column vectors. Users can interact with data frames through numerous operators to extract, add or recombine values. To extract single columns from a data frame R offers a very specific operator: the dollar $. It returns the column vector as indicated by its name based on a data frame preceding the $.

To see the $ operator in action let’s extract the population pop (in 1,000) from different states in the US based on the States dataset (from 1992) in the carData package:

States$pop
 [1]  4041   550  3665  2351 29760  3294  3287   666   607 12938
[11]  6478  1108  1007 11431  5544  2777  2478  3685  4220  1228
[21]  4781  6016  9295  4375  2573  5117   799  1578  1202  1109
[31]  7730  1515 17990  6629   639 10847  3146  2842 11882  1003
[41]  3487   696  4877 16987  1723   563  6187  4867  1793  4892
[51]   454

The command extracts the population column as vector from the data frame. From this vector we can calculate the sum() of the total population as:

sum(States$pop)
[1] 248709

Similarly, the average salary (in $1,000) for teachers can be calculated as the mean() from the pay column:

mean(States$pay)
[1] 30.94118

Quiz: Extract column from a data frame

      rank discipline yrs.since.phd yrs.service  sex salary
1     Prof          B            19          18 Male 139750
2     Prof          B            20          16 Male 173200
3 AsstProf          B             4           3 Male  79750
4     Prof          B            45          39 Male 115000
5     Prof          B            40          41 Male 141500
 [ reached 'max' / getOption("max.print") -- omitted 392 rows ]
Which R command can be used to calculate the average salary of professors in the Salaries dataset from the carData package?
  • mean(Salaries$salary)
  • mean(salary$Salaries)
  • Salaries(mean$salary)
  • TitanicSurvival(age$mean)
Start Quiz

Exercise: Extract column from a data frame

Calculate the average age of passengers in the TitanicSurvival dataset from the carData package. The carData package is already loaded.

Start Exercise

Extract or replace columns in a data frame using `$` is an excerpt from the course Introduction to R, which is available for free at quantargo.com

VIEW FULL COURSE

To leave a comment for the author, please follow the link and comment on their blog: Quantargo Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)