Re: [R] averaging rows on a data.frame according to a factor

2013-11-22 Thread arun
Hi, dat<-as.data.frame(cbind(c("a","a","a","b","b"), c(1,2,3,3,2),c(4,3,5,4,4)))  str(dat) #'data.frame':    5 obs. of  3 variables: # $ V1: Factor w/ 2 levels "a","b": 1 1 1 2 2 # $ V2: Factor w/ 3 levels "1","2","3": 1 2 3 3 2 # $ V3: Factor w/ 3 levels "3","4","5": 2 1 3 2 2 #better way would

Re: [R] averaging rows on a data.frame according to a factor

2013-11-22 Thread Rui Barradas
Sorry, there's a bug in my previous answer. dat <- data.frame(c("a","a","a","b","b"), c(1,2,3,3,2), c(4,3,5,4,4)) aggregate(as.matrix(dat[,-1]) ~ dat[,1], FUN = mean) Don't use as.data.frame(cbind(...)), everything becomes factors. Rui Barradas Em 22-11-2013 23:02, Rui Barradas escreveu: He

Re: [R] averaging rows on a data.frame according to a factor

2013-11-22 Thread Rui Barradas
Hello, Try the following. aggregate(cbind(V2, V3) ~ V1, dat, FUN = mean) Hope this helps, Rui Barradas Em 22-11-2013 21:43, john d escreveu: Dear all, I apologize for the newbie question, but I'm stuck. I have a data frame in the following form: dat<-as.data.frame(cbind(c("a","a","a","b"

Re: [R] averaging rows on a data.frame according to a factor

2013-11-22 Thread David Winsemius
On Nov 22, 2013, at 1:43 PM, john d wrote: > Dear all, > > I apologize for the newbie question, but I'm stuck. > > I have a data frame in the following form: > > dat<-as.data.frame(cbind(c("a","a","a","b","b"), c(1,2,3,3,2),c(4,3,5,4,4))) > > I need a way to generate a new dataframe with the