S_U
S_U

Reputation: 3

R: select just the top 5 values and group the others

I am quite new to R and now I hope that you could help me. I have a dataset with 22 variables and more than 50000 rows. For further calculations I want to selecte the top 5 values in every column and delete the others. How can I do that?

Thanks for your help.

Upvotes: 0

Views: 145

Answers (1)

www
www

Reputation: 39154

You should give us a reproducible example.

We can use apply and sort to achieve this task. Assuming that dat is the original data frame and dat2 is the final output.

set.seed(123)

dat <- as.data.frame(matrix(rnorm(50), ncol = 5))

#             V1         V2         V3          V4          V5
#  1  -0.56047565  1.2240818 -1.0678237  0.42646422 -0.69470698
#  2  -0.23017749  0.3598138 -0.2179749 -0.29507148 -0.20791728
#  3   1.55870831  0.4007715 -1.0260044  0.89512566 -1.26539635
#  4   0.07050839  0.1106827 -0.7288912  0.87813349  2.16895597
#  5   0.12928774 -0.5558411 -0.6250393  0.82158108  1.20796200
#  6   1.71506499  1.7869131 -1.6866933  0.68864025 -1.12310858
#  7   0.46091621  0.4978505  0.8377870  0.55391765 -0.40288484
#  8  -1.26506123 -1.9666172  0.1533731 -0.06191171 -0.46665535
#  9  -0.68685285  0.7013559 -1.1381369 -0.30596266  0.77996512
# 10  -0.44566197 -0.4727914  1.2538149 -0.38047100 -0.08336907

dat2 <- as.data.frame(apply(dat, 2, function(x) sort(x, decreasing = TRUE)[1:5]))
dat2
#           V1        V2         V3        V4          V5
# 1 1.71506499 1.7869131  1.2538149 0.8951257  2.16895597
# 2 1.55870831 1.2240818  0.8377870 0.8781335  1.20796200
# 3 0.46091621 0.7013559  0.1533731 0.8215811  0.77996512
# 4 0.12928774 0.4978505 -0.2179749 0.6886403 -0.08336907
# 5 0.07050839 0.4007715 -0.6250393 0.5539177 -0.20791728

Upvotes: 2

Related Questions