Although our data table only has six columns, some data tables include hundreds. If we want to view just a few, we can use the dplyr select function. In the code below we select three columns, assign this to a new object and then filter the new object:
new_table <- select(murders, state, region, rate)
filter(new_table, rate <= 0.71)
#> state region rate
#> 1 Hawaii West 0.515
#> 2 Iowa North Central 0.689
#> 3 New Hampshire Northeast 0.380
#> 4 North Dakota North Central 0.595
#> 5 Vermont Northeast 0.320In the call to select, the first argument murders is an object, but state, region, and rate are variable names.
Run the sample code to see how select() function works.
library(dplyr)
library(dslabs)
data("murders")
# Add the rate column
murders <- mutate(murders, rate = total / population * 100000)
# Check murders to find that the new column is added.
head(murders)
# select new table with 4 columns
new_table <- select(murders, state, region, rate)
# filter new_table
filter(new_table, rate <= 0.71)