简体   繁体   中英

R: Make table with rownames for top n column values

I have a data frame with numeric count values. I would like to create a new table that has the rownames of the top n values for each column.

df <- data.frame(a = c(1,4,5,2), b = c(7,1,4,6), c = c(5,6,7,1)
rownames(df) <- c(w,x,y,z)

using n = 2 my desired output would be

a b c
y w y
x z z

I am able to extract the top 5 values for each column using the following df2 <- apply(df, 2, function(x) sort(unique(x),decreasing =TRUE)[1:5]) but I cannot figure out how to get the rownames that correspond to each value.

A possible solution:

df <- data.frame(a = c(1,4,5,2), b = c(7,1,4,6), c = c(5,6,7,1))
rownames(df) <- c("w","x","y","z")

f <- function(x) names(sort(x,decreasing = T))[1:2]

apply(df,2,f)

#>      a   b   c  
#> [1,] "y" "w" "y"
#> [2,] "x" "z" "x"

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