Sonu Mishra
Sonu Mishra

Reputation: 1779

Put an upper limit on the value of variable in R

How can I put an upper cap on the variable in R?

Suppose I have a data.frame with columns "a" and "b". Values of "b" range from 1:100. I want to make all values > 50 equal to 50.

I can write a loop and get this done, but I am in need of a function.

Upvotes: 2

Views: 12014

Answers (1)

SymbolixAU
SymbolixAU

Reputation: 26258

As highlighted by @Frank, use the parallel minima function, ?pmin

## example data
df <- data.frame(a = 1:100, b = 1:100)

df$b = pmin(df$b, 50)

pmin compares the input values and returns the minimum of them.

Upvotes: 7

Related Questions