Skip to Main Content

Learn R

This guide focuses on transformation and cleaning functions in R that are especially useful for working with tabular datasets.

Selecting (Keeping) Variables and Selecting Observations

To select certain variables (for example. AGEGRP, TOTINC & WEIGHT). We create a new vector called myvars and then assign a subset data frame using myvars.

Type:

myvars <- c(“AGEGRP”, “TOTINC”, “WEIGHT”)
new_data_frame <- df[myvars]

Other Methods

There is another method to subset variables based on index. Suppose we wish to select 1st and 5th thru 10th variables.

Type:

Selecting the first 5 observations.

Type:

Selecting first 5 observations and first 3 variables.

Type:

Finally, let’s try creating a new data frame with FEMALES (SEX = 1) earning more than 35000

# sub-setting based on values of variables

Type:

External resource for subsetting in R: http://www.statmethods.net/management/subset.html

Liaison Librarian

Profile Photo
Martin Morris
Contact:
Schulich Library of Physical Sciences, Life Sciences and Engineering
Macdonald-Stewart Library Building
809 rue Sherbrooke Ouest
Montréal, Québec H3A 0C1
(514) 398 8140
Website Skype Contact: martinatmcgill
Social: Twitter Page

McGill LibraryQuestions? Ask us!
Privacy notice