简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM