[BioC] Subsetting in R

Ramon Diaz-Uriarte rdiaz at cnio.es
Thu Sep 18 13:50:40 MEST 2003

Dear Mick,

Regarding your final question, you can do

result[t > 4, ]

However, a couple of additional comments:

> t <= mt.teststat(matrix, class, test="t", nonpara="y")

The expression above won't work as given: you need "<-", not "<=". I assume it 
was a typo when copying.

You are naming your object as "t"; I'd rather not do that, since "t" is also 
the name of a function (for transpose) and that can lead to annoying and hard 
to find weird things down the road (once you forget that, 200 lines above, 
you created an object named t).

Similar for matrix: matrix is the name of an often used function; don't use it 
to name an object you just created.

> result = cbind(matrix, t)

I'd also use here "<-" instead of "=". This is not only a matter of personal 
taste, but can also help track down bugs, do searches, etc. Use of "<-" vs. 
"=" is discussed in several R/S manuals, books, etc, and comes up frequently 
in the R-help list.

Finally, it might be more convenient if you store everything as a data frame 
with named columns. For instance:

x <- matrix(rnorm(20), ncol = 4)
class.labels <- c(1, 1, 0, 0)
t1 <- mt.teststat(x, class.labels, test = "t", nonpar = "y")
my.results <- data.frame(x, statistic = t1)

Now you can do things such as:

my.results[my.results$statistic > 2,]
subset(my.results, statistic > 2)

Anyway, subsetting matrices and other objects, and naming objects one has 
created are fairly common operations in R; I'd strongly suggest you take a 
look at one (or more) of the available documents. If impatient, take a look 
at "A guide for the unwilling R user". And then move on to "An introduction 
R", and/or the other documents.

Hope this helps,


> So as far as I can tell i now have a matrix, "result", that has my
> normalised M values as columns with a final column on the end giving a
> non-parametric t-score for each row.
> I now want to subset this matrix so that I have only, say, those rows where
> t > 4.  Anyone have any idea how to do this?  I have tried all sorts of
> combinations of [,] on result and got nowhere.  I think I am using the
> wrong kind of object to do subsetting on :-(
> Thanks in advance for any help
> Mick
> _______________________________________________
> Bioconductor mailing list
> Bioconductor at stat.math.ethz.ch
> https://www.stat.math.ethz.ch/mailman/listinfo/bioconductor

Ramón Díaz-Uriarte
Bioinformatics Unit
Centro Nacional de Investigaciones Oncológicas (CNIO)
(Spanish National Cancer Center)
Melchor Fernández Almagro, 3
28029 Madrid (Spain)
Fax: +-34-91-224-6972
Phone: +-34-91-224-6900


More information about the Bioconductor mailing list