Hi Andre,
A slight correction: 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, drop = FALSE]/mean(x1[, 1]) ###changed } }) res <- data.frame(lst1[sapply(lst1, length) > 0]) colnames(res) <- gsub(".*\\.", "", colnames(res)) res } Also, you can try: fun2 <- function(beginColumn, by, data) { indx <- seq(beginColumn, ncol(data), by = by) indx1 <- head(indx + 1, -1) indx2 <- tail(indx - 1, -1) vec1 <- as.vector(sapply(seq_along(indx1), function(i) indx1[i]:indx2[i])) if (ncol(data) > tail(indx, 1)) { vec2 <- c(vec1, (tail(indx, 1) + 1):ncol(dat1)) } else { vec2 <- vec1 } means1 <- rep(colMeans(data[, indx]), each = by - 1, length.out = length(vec2)) res <- as.data.frame(t(t(data[, vec2])/means1)) res } set.seed(458) dat1 <- as.data.frame(matrix(sample(5,10*5,replace=TRUE),ncol=10)) identical(fun1(2,4,dat1),fun2(2,4,dat1)) #[1] TRUE A.K. 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.zacha...@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@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. ______________________________________________ R-help@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.