There are *many* ways, but here's two: df = data.frame(x = c(1:10), y = rnorm(10,2,1), label = rep(c('a', 'b', 'c', 'd', 'e'),2))
with(df, ave(x, label)) # Returns the correct value in each spot (useful if you want to add a group-mean column to df with(df, tapply(x, label, mean)) # Probably more what you were looking for. Michael On Thu, Feb 2, 2012 at 10:47 AM, Martin Batholdy <batho...@googlemail.com> wrote: > Hi, > > > I have the following data.frame: > > data.frame(x = c(1:10), y = rnorm(10,2,1), label = rep(c('a', 'b', 'c', 'd', > 'e'),2)) > > in this data.frame there is a label-variable containing strings. > Each string is represented two times. > > Now I would like to have the mean of the corresponding x (and y-values) for > every unique label-element. > > For the label 'a' for example there is an x value of 1 and 6. > So the resulting value should be 3.5. > > How can I do this in R? > > ______________________________________________ > 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.