R得到max,由另一个函数键入(比如在Python中)

时间:2016-04-19 19:56:15

标签: r max

在Python中,我们可以做类似

的事情
max(stuff, key=lambda x: abs(x.foo))

哪个会返回stuff的元素,其中成员foo的绝对值最高。

我如何在R?

中这样做

1 个答案:

答案 0 :(得分:0)

所以我认为东西必须是带有命名元素的向量(或列表)列表,如下所示:

04:00:07 27
Traceback (most recent call last):
  File "sawe_issue.py", line 16, in <module>
    plt.plot(time, data_size)
  File "C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 3099, in plot
    ret = ax.plot(*args, **kwargs)
  File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 1373, in plot
    for line in self._get_lines(*args, **kwargs):
  File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 304, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 263, in _plot_args
    linestyle, marker, color = _process_plot_format(tup[-1])
  File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 115, in _process_plot_format
    'Unrecognized character %c in format string' % c)
ValueError: Unrecognized character 7 in format string

你可以得到元素&#34; foo&#34;使用sapply:

ngRepeat

...然后找到它的最大值:

stuff <- list( first = c(bang=1, qux = 2, foo = 3),
               second = c(bang=6, qux = 0, foo= 100),
               third = c(bang = 1, qux = 7, foo = 0))

...然后用它来索引你的列表:

sapply(stuff, function(.) .['foo'])

或与magrittr:

which.max(sapply(stuff, function(.) .['foo']))