dataset with subset of variables in r

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.r
# create dataset
mydata <- data.frame(age = c(18, 21, 45, 32),
                     gender = c("M", "F", "M", "F"),
                     income = c(50000, 60000, 80000, 70000),
                     education = c("High school", "College", "Graduate", "PhD"))

# retain subset of variables
subset_mydata <- subset(mydata, select = c("age", "gender", "income"))
359 chars
9 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