To create a subset of variables from a larger dataset in R, you can use the subset
function. The subset
function creates a new data frame by retaining only the variables specified in the function call.
Here is an example code for creating a subset of variables from a larger dataset called mydata
. In this example, we want to retain only the variables age
, gender
, and income
.
main.r359 chars9 lines
In the above code, the subset
function is passed two arguments. The first argument is the dataset mydata
. The second argument, select
, specifies the variables we want to retain in the new dataset. The select
argument is set to a vector of variable names we want to retain.
The resulting data frame will only contain the variables age
, gender
, and income
and will exclude the education
variable.
gistlibby LogSnag