Andre Zacharia
2014-Apr-20 09:52 UTC
[R] to divide column cells by the mean of another column
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]]
Bert Gunter
2014-Apr-20 14:14 UTC
[R] to divide column cells by the mean of another column
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. >
Hi, May be this helps: fun1 <- function(beginColumn, by, data) { ??? indx <- seq(beginColumn, ncol(data), by = by) ??? dataNew <- data[, indx[1]:ncol(data)] ??? indx1 <- cumsum(seq(ncol(data)) %in% indx) ??? indx2 <- indx1[indx1 != 0] ??? lst1 <- lapply(split(seq_along(indx2), indx2), function(i) { ??????? x1 <- dataNew[, i, drop = FALSE] ??????? if (ncol(x1) > 1) { ??????????? x1[, -1]/mean(x1[, 1]) ??????? } ??? }) ??? res <- data.frame(lst1[sapply(lst1, length) > 0]) ??? colnames(res) <- gsub(".*\\.", "", colnames(res)) ??? res } set.seed(458) dat1 <- as.data.frame(matrix(sample(5,10*5,replace=TRUE),ncol=10)) set.seed(34) dat2 <- as.data.frame(matrix(sample(20,21*5,replace=TRUE),ncol=21)) ?fun1(2,4,dat1) fun1(2,5,dat2) A.K. On Sunday, April 20, 2014 5:55 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.