Hi All I have a data frame in which there are 4 columns . Column 1 : name Column 2-4 : values I would like to calculate mean/Standard error of values in column 2-4 and store them in column 5,6 respectively. I have done the following but doesn't seem to work mean_N_SE <-function(x) { name <- x[1] vals <- c(x[2:4]) temp_mean <- mean(vals) SE <- sqrt(var(x)/length(x)) } apply(d,1,mean_N_SE) where d = data frame. Can someone help me with this. Thanks! -Abhi [[alternative HTML version deleted]]
Look at the apply function ?apply x = data.frame(x1=c(1,2,3,4,5),x2=c(2,4,6,8,10),x3=c(1,3,5,7,9)) x$x5=apply(x,1,mean) x$x6=apply(x,1,sd) print(x) Abhishek Pratap wrote:> > Hi All > > I have a data frame in which there are 4 columns . > > Column 1 : name > > Column 2-4 : values > > I would like to calculate mean/Standard error of values in column 2-4 and > store them in column 5,6 respectively. > > > > I have done the following but doesn't seem to work > > mean_N_SE <-function(x) > { > > name <- x[1] > vals <- c(x[2:4]) > temp_mean <- mean(vals) > SE <- sqrt(var(x)/length(x)) > > } > > apply(d,1,mean_N_SE) where d = data frame. > > > Can someone help me with this. > > Thanks! > -Abhi > > [[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. > >-- View this message in context: http://n4.nabble.com/Operating-on-each-row-of-data-frame-tp1013365p1013397.html Sent from the R help mailing list archive at Nabble.com.
Hi, does this do what you want? d <- cbind(d,apply(d[,c(2,3,4)],1,mean),apply(d[,c(2,3,4)],1,sd)) HTH, Stephan Abhishek Pratap schrieb:> Hi All > > I have a data frame in which there are 4 columns . > > Column 1 : name > > Column 2-4 : values > > I would like to calculate mean/Standard error of values in column 2-4 and > store them in column 5,6 respectively. > > > > I have done the following but doesn't seem to work > > mean_N_SE <-function(x) > { > > name <- x[1] > vals <- c(x[2:4]) > temp_mean <- mean(vals) > SE <- sqrt(var(x)/length(x)) > > } > > apply(d,1,mean_N_SE) where d = data frame. > > > Can someone help me with this. > > Thanks! > -Abhi > > [[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. >
Thanks all for a very quick solution. It is actually good to know different ways to do the same things. It expands my limited understanding of R :). -A On Wed, Jan 13, 2010 at 5:12 PM, Stephan Kolassa <Stephan.Kolassa@gmx.de>wrote:> Hi, > > does this do what you want? > > d <- cbind(d,apply(d[,c(2,3,4)],1,mean),apply(d[,c(2,3,4)],1,sd)) > > HTH, > Stephan > > > Abhishek Pratap schrieb: > >> Hi All >> >> I have a data frame in which there are 4 columns . >> >> Column 1 : name >> >> Column 2-4 : values >> >> I would like to calculate mean/Standard error of values in column 2-4 and >> store them in column 5,6 respectively. >> >> >> >> I have done the following but doesn't seem to work >> >> mean_N_SE <-function(x) >> { >> >> name <- x[1] >> vals <- c(x[2:4]) >> temp_mean <- mean(vals) >> SE <- sqrt(var(x)/length(x)) >> >> } >> >> apply(d,1,mean_N_SE) where d = data frame. >> >> >> Can someone help me with this. >> >> Thanks! >> -Abhi >> >> [[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. >> >> >[[alternative HTML version deleted]]