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.
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.
Selecting the first 5 observations.
Selecting first 5 observations and first 3 variables.
Finally, let’s try creating a new data frame with FEMALES (SEX = 1) earning more than 35000
# sub-setting based on values of variables
External resource for subsetting in R: http://www.statmethods.net/management/subset.html
McGill Library • Questions? Ask us!Privacy notice