## Extract columns with the $___$___
___$___ <- ___ View Interactive Version 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:

Input
States$pop Output  [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: Input sum(States$pop)
Output
[1] 248709

Similarly, the average salary (in $1,000) for teachers can be calculated as the mean() from the pay column: Input mean(States$pay)
Output
[1] 30.94118
Extract or replace columns in a data frame using \$