Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in R Programming by (50.2k points)
In R, I am having a matrix of numeric values and want to display only the highest 20% of a specific column. How to perform that?

1 Answer

0 votes
by (107k points)

In R programming, you can use the quantile() to create a logical vector and extract the elements from the column:

m1[,1][m1[,1] >= quantile(m1[,1], 0.8)]

If it is a data.frame then in that case you can use top_frac:

library(dplyr)

as.data.frame(m1) %>%

    top_frac(n = 0.2, wt = col1)

Or you can use the slice_max

as.data.frame(m1) %>%

     slice_max(col1, prop = 0.2)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...