是否有向量化的并行 max() 和 min()? [英] Is there a vectorized parallel max() and min()?

查看:27
本文介绍了是否有向量化的并行 max() 和 min()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有a"和b"列的data.frame.我想添加名为高"和低"的列,其中包含列 a 和 b 中的最高和最低.

I have a data.frame with columns "a" and "b". I want to add columns called "high" and "low" that contain the highest and the lowest among columns a and b.

有没有办法在不遍历数据帧中的行的情况下执行此操作?

Is there a way of doing this without looping over the lines in the dataframe?

这是用于 OHLC 数据,因此高和低列应包含同一行上 a 和 b 之间的最高和最低元素,而不是整个列中.抱歉,如果措辞不好.

edit: this is for OHLC data, and so the high and low column should contain the highest and lowest element between a and b on the same line, and not among the whole columns. sorry if this is badly worded.

推荐答案

听起来您正在寻找 pmaxpmin(并行"最大值/最小值):

Sounds like you're looking for pmax and 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).

这篇关于是否有向量化的并行 max() 和 min()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆