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]

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
McGill Libraries • Questions? Ask us!
Privacy notice