O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Excluding columns

Use the minus sign for the variable positions that you want to exclude from the subset. Also, you cannot mix both positive and negative indices in the list. Both of the following approaches are correct:

> auto[,c(-1,-9)] > auto[,-c(1,9)] 

However, this subsetting approach does not work when specifying variables using names. For example, we cannot use -c("No", "car_name"). Instead, use %in% with ! (negation) to exclude variables:

> auto[, !names(auto) %in% c("No", "car_name")] 

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required