[R] Averaging within a range of values

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Fri Jan 13 16:05:26 CET 2012


Regarding your last question, read ?cut
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

doggysaywhat <chwhite at ucsd.edu> wrote:

>Hello all.
>
>I have two data frames.  
>Group       Start          End
>G1	               200	         700
>G2	               500	         1000
>G3	               2000        3000
>G4	               4000        6000
>G5	               7000        8000
>
>
>and
>
>Pos		    C0		    C1
>200		    0.9	  	  0.6
>500	   	  0.8	   	  0.8
>800		    0.9	  	  0.7
>1000		  0.7	  	0.6
>2000	  	0.6		  0.4
>2500	  	1.2	  	0.8
>3000	  	0.6	  	1.5
>3500	  	0.7	  	0.7
>4000	  	0.8		  0.8
>4500	  	0.6	  	0.6
>5000		  0.9	  	0.9
>5500	  	0.7		  0.8
>6000	  	0.8	  	0.7
>6500	  	0.4	  	0.4
>7000		  0.5	  	0.8
>7500  		0.7		  0.9
>8000	  	0.9	  	0.5
>8500	  	0.8	  	0.6
>9000	  	0.9	  	0.8
>
>
>I need to conditionally average all values in columns C0 and C1 based
>upon
>the bins I defined in the first data frame.  For example, for the bin
>G1 in
>the first dataframe, the values are 200 to 700 so i would average the
>value
>at pos 200 (0.9) and 500 (0.8) for C0 and then perform the same thing
>for
>C1.  
>
>I can do this in excel with array formulas but I'm relatively new to R
>and
>would like know if there is a function that will perform the same
>action.  I
>don't know if this will help, but the excel array function I used was
>average(if(range>=start)*(range<=end),range)).  Where the range is the
>entire pos column.  
>
>Initially I looked at the aggregate function.   I can use aggregate
>when I
>give a single vector to be used for grouping such as (A,B,C) but I'm
>not
>sure how to define grouping as the bin 200-500 and the second bin as
>500-1000 etc. and use that as my grouping vector.   
>
>Any help would be greatly appreciated.  
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/Averaging-within-a-range-of-values-tp4291958p4291958.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>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.



More information about the R-help mailing list