Home > Enterprise >  R: Make table with rownames for top n column values
R: Make table with rownames for top n column values

Time:11-02

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.

CodePudding user response:

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"
  • Related