[R] Removing columns from a matrix using a list

Gabor Grothendieck ggrothendieck at gmail.com
Tue Mar 28 18:45:59 CEST 2006

Here are three ways:

subset(iris, select = -c(Sepal.Length, Species))

nm <- c("Sepal.Length", "Species")
iris[,!names(iris) %in% nm]

nm <- c("Sepal.Length", "Species")
iris[,setdiff(names(iris), nm)]

On 3/28/06, Peter Wilkinson <pwilkinson_m at xbioinformatics.org> wrote:
> How does one remove columns either by name ("Exp1", "Exp2", "Exp3", ...
> "Exp100") or by column number from a matrix or data.frame using a list of
> names mynames = ("Exp10,"Exp20","Exp55","Exp67")
> I would something in the form of (I know this does not work):
> myMatrixEdited = myMatrix[,-mynames]
> Or I would like to find the column numbers of the names in the list, and use
> the column numbers to remove the columns in the matrix or data.frame.
> Peter W.
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list