[R] to divide column cells by the mean of another column
Bert Gunter
gunter.berton at gene.com
Sun Apr 20 16:14:26 CEST 2014
R has no "cells".
You need to do your homework by reading "An Introduction to R" , which
ships with R, or one of the many R web tutorials of your choice. What
you describe is trivial once you have made a minimal effort to learn
R. In particular, ?"[" explains how to index data frames; but a
tutorial is a better option for a learner.
-- Bert
Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374
"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
H. Gilbert Welch
On Sun, Apr 20, 2014 at 2:52 AM, Andre Zacharia
<andre.zacharia at gmail.com> wrote:
> Dear all,
>
> I am getting data columnwise that I need to divide by the mean of another
> column
>
> If the column is the previous one this code works perfectly well:
>
> fun1 <- function(beginColumn, by, data) { indx <- seq(beginColumn,
> ncol(data), by = by) as.data.frame(t(100 - (t(data[, indx])/colMeans(data[,
> indx - 1], na.rm = TRUE)) * 100))
> }
> (Arun helped me with this code, thank you again!...)
>
> But, the things is now more complicated...
>
> I need to program a function that allow me to divide for example cells from
> column 3 on mean from column 2 and cells from column 4 on mean of column 2
> and the 5 etc. Then column 6 is another column from whch I need to extract
> the mean and to do the same with column 7 and 8, etc...
>
> so if I have:
>
> 1 2 3 4 1 5
> 2 5 4 7 2 8
> 3 4 5 9 3 7
> 4 7 7 9 4 3
>
> The serie 1,2,3,4 ar just enumerating so not useful at this timepoint.
>
> the results should be (from excel...):
> 4,5 33,3333333 11,1111111 -11,1111111 11,1111111 -55,5555556 -77,7777778
> -11,1111111 -100 -55,5555556 -55,5555556 -100
> 33,3333333
> I tried to work on modyfying indx-1 by 2*indx-2, but this is not doing the
> job... I tried many other things so that I am now stucked.
>
> Does Anyone has a brilliant idea?
>
> Many many thanks
>
> André ZACHARIA
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list