[R] The unary - operator and matrix column labels

David Brahm brahm at alum.mit.edu
Fri Aug 9 16:33:46 CEST 2002

Kort, Eric <Eric.Kort at vai.org> writes:
>   ig1 <- c('patient1', 'patient2')
> then, while I can do this:
>   data[,ig1]
> I can no longer do this:
>   data[,-ig1]
> because the unary operator does not like working with column labels.  Is
> there a way to go backwards and determine the column indices from the column
> labels...?

If you know "ig1" is indeed a column of the matrix "data", this should work:

R> data[ , -match(ig1, colnames(data))]

If you don't know that for sure, and/or you want to preserve the matrix
structure of the result (drop=F), then here's a fancier version:

R> data[ , setdiff(colnames(data), ig1), drop=F]
                              -- David Brahm (brahm at alum.mit.edu)
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list