How to control the maximum number in a dataframe

I have this dataframe, which is like a matrix. I want to limit the maximum value in df to 50, how to do this? In the resulting dataframe, 120, 110, 200 will be changed to 50. I couldn't find any source online. Thanks for your help.

df= data.frame(v1=c(1.2, 0, 1, 120),v2=c(3.5, 110, 0.8, 3),v3=c(200, 4.1, 0, 3.3))

Strangely simple. I only recently learned this works.

df= data.frame(v1=c(1.2, 0, 1, 120),v2=c(3.5, 110, 0.8, 3),v3=c(200, 4.1, 0, 3.3))
df
#>      v1    v2    v3
#> 1   1.2   3.5 200.0
#> 2   0.0 110.0   4.1
#> 3   1.0   0.8   0.0
#> 4 120.0   3.0   3.3
df[df > 50] <- 50
df
#>     v1   v2   v3
#> 1  1.2  3.5 50.0
#> 2  0.0 50.0  4.1
#> 3  1.0  0.8  0.0
#> 4 50.0  3.0  3.3

Created on 2019-12-10 by the reprex package (v0.3.0.9000)

1 Like

It works. Thanks a million.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.