Is there a vectorized parallel max() and min()?

后端 未结 4 1530
滥情空心
滥情空心 2020-12-01 11:49

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

4条回答
  •  一生所求
    2020-12-01 12:17

    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).
    

提交回复
热议问题