[R] Finding a max

Rui Barradas ruipbarradas at sapo.pt
Wed Nov 21 18:51:42 CET 2012


Hello,

You're complicating what is simple:


prop3$effort[which.max(prop3$Low)]  # First maximum of Low
prop3$effort[which.max(prop3$High)] # Ditto, of High

which.max(prop3$Low)   # Row number that maximizes Low
which.max(prop3$High)  # Row number that maximizes High


Hope this helps,

Rui Barradas

Em 21-11-2012 17:10, Ignacio Martinez escreveu:
> My data looks like this:
>
> X Y1(X) Y2(X)
>
> i want to find the values of x that maximize Y1 and Y2.
> Right now I'm getting the answer but I would like to know if there is a
> more efficient/elegant way of doing this.
>
> This code reproduces what I'm doing:
> [code]
> prop3<-structure(list(effort = c(0, 0.008989899, 0.017979798, 0.026969697,
>                            0.035959596, 0.044949495, 0.053939394,
> 0.062929293, 0.071919192,
>                            0.080909091, 0.08989899, 0.098888889,
> 0.107878788, 0.116868687,
>                            0.125858586, 0.134848485, 0.143838384,
> 0.152828283, 0.161818182,
>                            0.170808081, 0.17979798, 0.188787879,
> 0.197777778, 0.206767677,
>                            0.215757576, 0.224747475, 0.233737374,
> 0.242727273, 0.251717172,
>                            0.260707071, 0.26969697, 0.278686869,
> 0.287676768, 0.296666667,
>                            0.305656566, 0.314646465, 0.323636364,
> 0.332626263, 0.341616162,
>                            0.350606061, 0.35959596, 0.368585859,
> 0.377575758, 0.386565657,
>                            0.395555556, 0.404545455, 0.413535354,
> 0.422525253, 0.431515152,
>                            0.440505051, 0.44949495, 0.458484849,
> 0.467474748, 0.476464647,
>                            0.485454546, 0.494444445, 0.503434344,
> 0.512424243, 0.521414142,
>                            0.530404041, 0.53939394, 0.548383839,
> 0.557373738, 0.566363637,
>                            0.575353536, 0.584343435, 0.593333334,
> 0.602323233, 0.611313132,
>                            0.620303031, 0.62929293, 0.638282829,
> 0.647272728, 0.656262627,
>                            0.665252526, 0.674242425, 0.683232324,
> 0.692222223, 0.701212122,
>                            0.710202021, 0.71919192, 0.728181819,
> 0.737171718, 0.746161617,
>                            0.755151516, 0.764141415, 0.773131314,
> 0.782121213, 0.791111112,
>                            0.800101011, 0.80909091, 0.818080809,
> 0.827070708, 0.836060607,
>                            0.845050506, 0.854040405, 0.863030304,
> 0.872020203, 0.881010102,
>                            0.890000001), Low = c(7118.22889879,
> 7198.74588723, 7202.19756567,
>                                                  7205.63654441,
> 7209.06211889, 7212.47354473, 7215.87003521, 7219.25075859,
>                                                  7222.61483517,
> 7225.96133418, 7229.28927041, 7232.59760064, 7235.88521975,
>                                                  7239.15095652,
> 7242.39356918, 7245.61174055, 7248.8040728, 7251.96908185,
>                                                  7255.10519126,
> 7258.21072566, 7261.28390362, 7264.32282997, 7267.32548746,
>                                                  7270.28972767,
> 7273.21326125, 7276.0936472, 7278.92828132, 7281.7143836,
>                                                  7284.44898447,
> 7287.12890981, 7289.75076469, 7292.31091545, 7294.80547025,
>                                                  7297.23025772,
> 7299.5808036, 7301.85230504, 7304.03960253, 7306.13714891,
>                                                  7308.13897531,
> 7310.03865366, 7311.82925527, 7313.50330516, 7315.05273141,
>                                                  7316.46880922,
> 7317.74209876, 7318.86237619, 7319.81855692, 7320.59861012,
>                                                  7321.18946333,
> 7321.57689588, 7321.74541961, 7321.678145, 7321.35663099,
>                                                  7320.76071577,
> 7319.86832617, 7318.65526223, 7317.09495336, 7315.15818179,
>                                                  7312.81276812,
> 7310.02321309, 7306.7502885, 7302.95056882, 7298.57589369,
>                                                  7293.57274947,
> 7287.88155557, 7281.43583878, 7274.16127505, 7265.97457404,
>                                                  7256.78217653,
> 7246.47872816, 7234.94528475, 7222.04719437, 7207.63158811,
>                                                  7191.52439555,
> 7173.52677949, 7153.41085818, 7130.91454853, 7105.73531868,
>                                                  7077.52257998,
> 7045.86837049, 7010.29587987, 6970.24522727, 6925.05571843,
>                                                  6873.94355449,
> 6815.97361626, 6750.02346243, 6674.73699741, 6588.46429282,
>                                                  6489.18264441,
> 6374.39189638, 6240.97402156, 6085.00235313, 5901.47881048,
>                                                  5683.96641688,
> 5424.06674216, 5110.66298423, 4728.80081891, 4257.99514316,
>                                                  3669.60082983,
> 2922.6078062), High = c(7118.22889879, 7198.73069287,
>
>             7202.13567036, 7205.49469474, 7208.80521654, 7212.06454229,
> 7215.26982532,
>
>             7218.41805599, 7221.50605116, 7224.53044288, 7227.48766626,
> 7230.37394647,
>
>             7233.18528464, 7235.91744284, 7238.56592778, 7241.12597328,
> 7243.59252142,
>
>             7245.96020213, 7248.22331114, 7250.3757862, 7252.41118136,
> 7254.32263905,
>
>             7256.1028599, 7257.74407005, 7259.23798555, 7260.57577384,
> 7261.74801177,
>
>             7262.74464001, 7263.5549133, 7264.16734637, 7264.56965475,
> 7264.74869031,
>
>             7264.69037066, 7264.37960193, 7263.80019421, 7262.93476875,
> 7261.76465611,
>
>             7260.26978417, 7258.42855498, 7256.2177089, 7253.61217492,
> 7250.58490516,
>
>             7247.1066919, 7243.14596494, 7238.6685668, 7233.63750309,
> 7228.01266486,
>
>             7221.75051921, 7214.80376422, 7207.12094325, 7198.64601318,
> 7189.31786036,
>
>             7179.06975677, 7167.82874812, 7155.51496388, 7142.04083786,
> 7127.31022594,
>
>             7111.21740525, 7093.6459365, 7074.46736768, 7053.53975373,
> 7030.70596185,
>
>             7005.79172659, 6978.60341186, 6948.92542873, 6916.51724768,
> 6881.10993137,
>
>             6842.40209847, 6800.05521033, 6753.68804819, 6702.87021938,
> 6647.11449352,
>
>             6585.86772328, 6518.50004474, 6444.29197687, 6362.41894264,
> 6271.93260934,
>
>             6171.73828277, 6060.56737789, 5936.94370783, 5799.14196159,
> 5645.13624236,
>
>             5472.53586574, 5278.5047004, 5059.65907105, 4811.93748795,
> 4530.43299682,
>
>             4209.17542618, 3840.84573636, 3416.39725665, 2924.54758403,
> 2351.08829921,
>
>             1677.93413337, 881.793254374, -67.723576975, -1210.84187318,
>
>             -2601.67300103, -4314.3411342, -6452.49702682, -9164.53328152
>                                                  )), .Names = c("effort",
> "Low", "High"), class = "data.frame", row.names = c(NA,
>
>                                                   -100L))
> print(prop3$effort[seq(along=prop3$High)[prop3$High == max(prop3$High)]])
> print(prop3$effort[seq(along=prop3$Low)[prop3$Low == max(prop3$Low)]])
>
>
> [/code]
> Thanks a lot!
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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