Subsetting in R
1
0
Entering edit mode
@michael-watson-iah-c-378
Last seen 10.2 years ago
I don't really understand the subsetting in R. I have a matrix of normalised M values: matrix <- maM(norm.data) I ran a t-test on these using multtest and combined the results into another matrix: t <= mt.teststat(matrix, class, test="t", nonpara="y") result = cbind(matrix, t) 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
multtest multtest • 717 views
ADD COMMENT
0
Entering edit mode
Ramon Diaz ★ 1.1k
@ramon-diaz-159
Last seen 10.2 years ago
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,] ##or 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, Ram?n > > 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@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 http://bioinfo.cnio.es/~rdiaz
ADD COMMENT

Login before adding your answer.

Traffic: 847 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6