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?
andre.zacharia at gmail.com
2014-Apr-13 12:01 UTC
[R] mean calculations from a dframe column
Thank you very much!!!!!! You saved me a lot of time now! When you look for the key for hours.... Many thank again André *De :* arun kirshna [via R] *Envoyé :* 13 avril 2014 11:23 *À :* andre.zacharia@gmail.com *Objet :* Re: mean calculations from a dframe column 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é ______________________________________________ [hidden email] <http://user/SendEmail.jtp?type=node&node=4688694&i=0>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. ------------------------------ If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/mean-calculations-from-a-dframe-column-tp4688674p4688694.html To unsubscribe from mean calculations from a dframe column, click here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4688674&code=YW5kcmUuemFjaGFyaWFAZ21haWwuY29tfDQ2ODg2NzR8LTE5ODcxMTY1OTA=> . NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble:email.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble:email.naml-instant_emails%21nabble:email.naml-send_instant_email%21nabble:email.naml> ----- André -- View this message in context: http://r.789695.n4.nabble.com/mean-calculations-from-a-dframe-column-tp4688674p4688696.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]