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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With