4.1 Select a subset of variables
To limit your dataset to a subset of variables in base R, use brackets [ ]
or subset()
.
## [1] "ID" "Age" "AgeGp" "Sex" "Yrs_From_Dx"
## [6] "CDAI" "CDAI_YN" "DAS_28" "DAS28_YN" "Steroids_GT_5"
## [11] "DMARDs" "Biologics" "sDMARDS" "OsteopScreen" "FIPS"
## [1] "ID" "Age" "CDAI"
# Select a subset of variables using subset()
subdat <- subset(mydat,
select = c(ID, Age, CDAI))
names(subdat)
## [1] "ID" "Age" "CDAI"
## [1] "ID" "Age" "AgeGp" "Sex" "Yrs_From_Dx"
## [6] "CDAI"
## [1] "ID" "Age" "AgeGp" "Sex" "Yrs_From_Dx"
In tidyverse
, use select()
. As with subset()
, you name the variables you want to keep, without quotes, or precede with a minus sign the names of variables you want to drop.