# [R] mean calculations from a dframe column

arun smartpink111 at yahoo.com
Sun Apr 13 12:11:54 CEST 2014

```Hi André,

Your codes were missing in some information. If your code looks like this:

Measure <- function(a, b) { a <- as.matrix(a) b <- as.matrix(b) Mean <- apply(a, 2, mean, na.rm = TRUE) somme <- c() for (i in seq_along(b)) somme[i] <- divide(Mean, b[i]) somme <- as.data.frame(somme) return(somme)
} MeanManqu <- function(a) apply(a, 2, mean, na.rm = TRUE)
divide <- function(a, b) { 100 - (b/a * 100)
}

df <- read.table(text="a   b     c    d   e    f 1  1      4    1  2   54 2  2     33   2  56  32 3  3      5    3  87  24 4 NA     NA    4 76  21",sep="",header=TRUE)

as.data.frame(divide(MeanManqu(as.matrix(df[,2])), df[,3]))

#divide(MeanManqu(as.matrix(df[, 2])), df[, 3])
#1                                           -100

#2                                          -1550

#3                                           -150
#4                                             NA

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))
}

fun1(3,3,df)

#      c         f

#1  -100  2.262443

#2 -1550 42.081448
#3  -150 56.561086
#4    NA 61.990950

A.K.

Hello!! I am stucked..... I have a dataframe with missing values. I want to divide each cell of my data frame by the mean from the previous column. Nevertheless I have several columns. I have the code that works for comparing one column and the mean of the previous one, but how can i ask R to repaeat it for seveal columns? Here are the data: a   b     c    d   e    f...... 1  1      4    1  2   54 2  2     33   2  56  32 3  3      5    3  87  24 4 NA     NA    4 76  21 My idea is to create a new dataframe  with for example: 100-(c/mean(b)*100) in one column, and the same for 100-(f/mean(e)*100) as results , etc. in this case column "a" and column "d" are just enumerating and are not useful. Here are my codes that are working for one trial only, but then.... Helppppp divide<-function(a,b) { 100-(b/a*100) MeanManqu<-apply(a,2,mean, na.rm=TRUE) > Measure<-function(a,b){ + a<-as.matrix(a) + b<-as.matrix(b) + Mean<-apply(a,2,mean, na.rm=TRUE) + somme <- c() +
+ for (i in seq_along(b) ) somme[i] <- divide(Mean,b[i]) + somme<-as.data.frame(somme) + return(somme) + } I tried several kind of loops and also apply, tapply... but failed I tried also something like that if it helps... as.data.frame(divide(MeanManqu(as.matrix(Data[,2])), Data[,3])) but this does  work for only one trial... One of the codes I tried: MeasureGd<-function(C){ + + i<-c(3,6,9,12,15) #these are the columns i want to use for my analysis + somme<-c() + for (i in seq_along(C[,i])){ + + + + somme[i]<- as.data.frame(divide(MeanManqu(as.matrix(C[,i-1])), C[,i])) #dividing a column by the mean of the previous one... + + } + return(somme) + } If someone as an idea or the key of this problem, I would be more than grateful!!! André

```