Zoppoli, Gabriele (NIH/NCI) [G]
2010-Feb-13 17:39 UTC
[R] how to do calculations in data matrices?
Please give me just a reference where I can find something useful. In summary, I need to : - find the median of each row of a matrix - create a new matrix with each value in the first matrix divided by the median of its row - if a value "a" in the second matrix is < 1, I need to substitute it with 1/a I know that for some of you it must be overeasy, but I swear I googled for two hours with keywords "operations", "calculations", "data matrices", "data tables", and "CRAN", and I didn't find anything useful. Thank you all Gabriele Zoppoli, MD Ph.D. Fellow, Experimental and Clinical Oncology and Hematology, University of Genova, Genova, Italy Guest Researcher, LMP, NCI, NIH, Bethesda MD Work: 301-451-8575 Mobile: 301-204-5642 Email: zoppolig at mail.nih.gov
Some places to start apply() sweep() and any of the good introductory guides to R available online. For finding R material, it's vastly easier to use the custom search engine at www.rseek.org than to wander aimlessly around google trying to find things with "R" in them. We'd be happy to give more specific answers if you need them, especially if you do as the posting guide asks and provide a reproducible example. (Not that you need one for this question, since you did ask just for references.) Sarah On Sat, Feb 13, 2010 at 12:39 PM, Zoppoli, Gabriele (NIH/NCI) [G] <zoppolig at mail.nih.gov> wrote:> Please give me just a reference where I can find something useful. > > In summary, I need to : > > - find the median of each row of a matrix > - create a new matrix with each value in the first matrix divided by the median of its row > - if a value "a" in the second matrix is < 1, I need to substitute it with 1/a > > I know that for some of you it must be overeasy, but I swear I googled for two hours with keywords "operations", "calculations", "data matrices", "data tables", and "CRAN", and I didn't find anything useful. > > Thank you all > > > Gabriele Zoppoli, MD > Ph.D. Fellow, Experimental and Clinical Oncology and Hematology, University of Genova, Genova, Italy > Guest Researcher, LMP, NCI, NIH, Bethesda MD > > Work: 301-451-8575 > Mobile: 301-204-5642 > Email: zoppolig at mail.nih.gov > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Sarah Goslee http://www.stringpage.com http://www.astronomicum.com http://www.functionaldiversity.org
On Sat, Feb 13, 2010 at 5:39 PM, Zoppoli, Gabriele (NIH/NCI) [G] <zoppolig at mail.nih.gov> wrote:> Please give me just a reference where I can find something useful. >To complement Sarah's suggestions, a good place to start is Quick-R [1]. Liviu [1] http://www.statmethods.net/stats/descriptives.html
Zoppoli, Gabriele (NIH/NCI) [G] <zoppolig <at> mail.nih.gov> writes:> > Please give me just a reference where I can find something useful.The others are right that rather than randomly googling, you should bite the bullet and sit down for a couple of hours with some introductory material on R (a book, or one of the freely available pdfs). Unless you are never going to use R again, it will be worth it. But seeing as you asked your question clearly, here's one way to do the steps you specify. Hopefully this will help as well. First, make a matrix to work with:> mat1 <- matrix(sample(1:10, size=12, replace=TRUE), ncol=4) > mat1[,1] [,2] [,3] [,4] [1,] 5 7 10 9 [2,] 4 10 8 2 [3,] 8 10 9 5> > > In summary, I need to : > > - find the median of each row of a matrixYou can use apply for that:> row.medians <- apply(mat1, 1, median) > row.medians[1] 8.0 6.0 8.5> - create a new matrix with each value in the first matrix divided by > the median of its rowThat's easy to *do*:> mat2 <- mat1 / row.medians > mat2[,1] [,2] [,3] [,4] [1,] 0.6250000 0.875000 1.250000 1.1250000 [2,] 0.6666667 1.666667 1.333333 0.3333333 [3,] 0.9411765 1.176471 1.058824 0.5882353 but it may take more time to understand why that worked. How come it knew that we wanted to divide each row by the median of the row? (Hint: understand the "byrow" argument in ?matrix and the mentions of the word "recyling" in ?Arithmetic).> - if a value "a" in the second matrix is < 1, I need to substitute it > with 1/aFirst make a logical vector which identifies the elements of the matrix you want to operate on:> is.small <- mat2 < 1Then perform the operation on those elements:> mat2[is.small] <- 1 / mat2[is.small] > mat2[,1] [,2] [,3] [,4] [1,] 1.6000 1.142857 1.250000 1.125 [2,] 1.5000 1.666667 1.333333 3.000 [3,] 1.0625 1.176471 1.058824 1.700 # you could also use ifelse(mat2 < 1, 1/mat2, mat2) dan> > I know that for some of you it must be overeasy, but I swear I googled > for two hours with keywords "operations", "calculations", "data > matrices", "data tables", and "CRAN", and I didn't find anything > useful. > > Thank you all > > Gabriele Zoppoli, MD > Ph.D. Fellow, Experimental and Clinical Oncology and Hematology, > University of Genova, Genova, Italy > Guest Researcher, LMP, NCI, NIH, Bethesda MD > > Work: 301-451-8575 > Mobile: 301-204-5642 > Email: zoppolig <at> mail.nih.gov >