[R] R: Grouping columns in a data frame based on the values of a column

From: Guazzetti Stefano <Stefano.Guazzetti_at_ausl.re.it>
Date: Fri 15 Sep 2006 - 15:58:02 GMT


Perhaps using 'ave' and 'cut':

 df <- data.frame(x=runif(100, 0.1, 1), y=rnorm(100, 0.2, 0.6))  df$xcut<-cut(df$x, seq(0, 1, 0.1))
 df$z<-ave(df$y, df$xcut)
 df[order(df$x),]

Stefano

-----Messaggio originale-----
Da: r-help-bounces@stat.math.ethz.ch
[mailto:r-help-bounces@stat.math.ethz.ch]Per conto di e.rapsomaniki@mail.cryst.bbk.ac.uk
Inviato: venerd́ 15 settembre 2006 17.45 A: r-help@stat.math.ethz.ch
Oggetto: [R] Grouping columns in a data frame based on the values of a column

Dear R users,

This is a trivial question, there might even be an R function for it, but I have to do it many times and wonder if there is an efficient for it.

Suppose we have a data frame like this:
d <- data.frame(x=sample(seq(0.1:1, by=0.01), size=100, replace=TRUE), y=rnorm(100, 0.2, 0.6))

and want to have the average of y for a given interval of x, for example mean(y)[0>x>0.1]. Is there a simple way of doing this or I need to improvise?

Thank you so much for any help
Eleni Rapsomaniki



R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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. Received on Sat Sep 16 02:01:44 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 15 Sep 2006 - 18:30:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.