是否有向量化并行max()和min()?

我有一个包含列“a”和“b”的数据框。我想添加列“a”和“b”中包含最高和最低的列“high”和“low”。

有没有这样做的方式,而不是循环在数据帧中的行?

编辑:这是OHLC数据,因此高和低列应包含同一行上a和b之间的最高和最低元素,而不是整列之间。对不起,如果这是不好的措辞。

听起来你正在寻找pmax和pmin(“parallel”max / min):

Extremes                 package:base                  R Documentation

Maxima and Minima

Description:

     Returns the (parallel) maxima and minima of the input values.

Usage:

     max(..., na.rm = FALSE)
     min(..., na.rm = FALSE)

     pmax(..., na.rm = FALSE)
     pmin(..., na.rm = FALSE)

     pmax.int(..., na.rm = FALSE)
     pmin.int(..., na.rm = FALSE)

Arguments:

     ...: numeric or character arguments (see Note).

   na.rm: a logical indicating whether missing values should be
          removed.

Details:

     ‘pmax’ and ‘pmin’ take one or more vectors (or matrices) as
     arguments and return a single vector giving the ‘parallel’ maxima
     (or minima) of the vectors.  The first element of the result is
     the maximum (minimum) of the first elements of all the arguments,
     the second element of the result is the maximum (minimum) of the
     second elements of all the arguments and so on.  Shorter inputs
     are recycled if necessary.  ‘attributes’ (such as ‘names’ or
     ‘dim’) are transferred from the first argument (if applicable).
http://stackoverflow.com/questions/5591593/is-there-a-vectorized-parallel-max-and-min

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:是否有向量化并行max()和min()?