The select() function will select specific columns (variables) in a data frame. This may be useful when a data frame has many variables and you only want to take a look at a few of them together, or save a subset of variables as a new data frame.
NOTE: The select() function selects columns. To select specific rows, see the filter() function.
Example 1:
# Select only the columns for Thumb, Sex, and RaceEthnic in Fingers
# Optional: Use head() to truncate the output
head(select(Fingers, Thumb, Sex, RaceEthnic))
Example output:
Example 2:
# Save a selected set of variables as a new data frame called Fingers_subset
Fingers_subset <- select(Fingers, Thumb, Index, Middle, Ring, Pinkie)
head(Fingers_subset)
Example output:
Related Articles
Printing Pages of the Book
If you need to print pages of the book, you may find that printing them from Chrome results in oddly-formatted pages. You will get better-formatted pages if you print using either Safari or Firefox as your browser. If using Safari, go the the Safari ...
filter()
The filter() function will find rows/cases where the conditions indicated are true. It is often used with operators such as the following: > (greater than) < (less than) >= (greater than or equal to) <= (less than or equal to) == (equal to) != (not ...
recode()
The recode() function can be used to rename any of the values of a variable. Example: # recode the variable `Year` and save it is a new column in the data frame Fingers$Year_recode <- recode(Fingers$Year, "1" = "First", "2" = "Second", "3" = "Third", ...
predict()
The predict() function will generate the prediction(s) from a model. Example 1: # Calculate the predictions from the Sex model of Thumb # Use the lm() function to specify the model predict(lm(Thumb ~ Sex, data = Fingers)) # Alt: Save the model into ...
ntile()
The ntile() function can be used to create equal sized groups (n-tiles) out of a quantitative variable. It can be modified to make any number of groups. For instance, if you take a quantitative variable such as Height from the Fingers data frame, you ...