Re: Binning a dependent variable (CORRECTION)

*To*: mathgroup at smc.vnet.net*Subject*: [mg111972] Re: Binning a dependent variable (CORRECTION)*From*: Bob Hanlon <hanlonr at cox.net>*Date*: Sun, 22 Aug 2010 08:11:48 -0400 (EDT)

The explicit filtering of the data in the module was unnecessary, BinLists does that automatically. The function can be written more simply as avgByBin[ data_?MatrixQ, {tempMin_?NumericQ, tempMax_?NumericQ, tempBinWidth_?NumericQ}] := Module[{values = data[[All, 2]], tbw = Min[tempBinWidth, tempMax - tempMin]}, If[data == {}, {}, If[# == {}, {}, {Mean[#[[All, 1]]], Mean[#[[All, 2]]]}] & /@ Flatten[BinLists[ data, {tempMin, tempMax, tbw}, {Min[values], Max[values], Max[values] - Min[values]}], 1]]] /; tempMin < tempMax Bob Hanlon ---- Bob Hanlon <hanlonr at cox.net> wrote: ============= Clear[avgByBin] avgByBin[data_?MatrixQ, {tempMin_?NumericQ, tempMax_?NumericQ, tempBinWidth_?NumericQ}] := Module[ {filteredData, values = data[[All, 2]], tbw = Min[tempBinWidth, tempMax - tempMin]}, filteredData = Select[data, tempMin <= # [[1]] <= tempMax &]; If[filteredData == {}, {}, If[# == {}, {}, {Mean[#[[All, 1]]], Mean[#[[All, 2]]]}] & /@ Flatten[ BinLists[filteredData, {tempMin, tempMax, tbw}, {Min[values], Max[values], Max[values] - Min[values]}], 1]]] /; tempMin < tempMax data = Table[ {RandomReal[{-100, 1000}], RandomReal[]}, {10000}]; avgByBin[data, {0, 500, 100}] Bob Hanlon ---- northern_flicker <max_kip at hotmail.com> wrote: ============= Dear Forum I have data with x and y values, i.e., temperature and a variable dependent on the temperature, e.g., {{30, 0.0019}, {31, 0.0024}, {32, 0.0026}...{998,0.000005},{999,0.000004},{1000,0.000003}}, with, for example, 3000 measurements in total. I would like to: 1) Bin the temperature data (x) into bins of any desired width, plus 2) specify the temperature range over which to bin the data and 3) bin the corresponding dependent variable (y), so that I can calculate the corresponding mean y for the temperature bin, ultimately resulting in: {{31,0.00793}...{999,0.0000004}}, where x is now the mean temperature and y is the mean dependent variable for that temperature bin. I tried to manipulate BinLists and BinCounts to solve this problem, but ran into difficulties. Any help would be much appreciated