Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Get the min/max of each item of a vector compared to single value

I want to compare a single value with each item of a vector (data.frame column) and receive a new vector as result.

a <- data.frame(v=c(3,1,5))
n <- 4
b <- # get max of `a$v` and `n` and return a vector
#desired output:
#[1] 4 4 5

The normal max function does not work.

like image 303
Juve Avatar asked Dec 13 '13 13:12

Juve


People also ask

How do you find the max and min of a vector in R?

We can find the minimum and the maximum of a vector using the min() or the max() function. A function called range() is also available which returns the minimum and maximum in a two element vector.

How do you find max and min in R?

The max() function in R computes the maximum value of a vector. The min() function in R computes the minimum value of a vector. The maximum of the group is also calculated using the max() function.

How do you find the max and min of a column in R?

min() function in R computes the minimum value of a vector or data frame. max() function in R computes the maximum value of a vector or data frame. column wise maximum and minimum of the dataframe using max() and min() function. Row wise maximum and minimum of the dataframe in R using max() and min() function.


1 Answers

I'm guessing you're looking for pmin/pmax:

> pmin(a$v, n)
[1] 3 1 4
> pmax(a$v, n)
[1] 4 4 5
like image 162
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 26 '22 15:10

A5C1D2H2I1M1N2O1R2T1