0

I want to get the highest values (lets say highest 3) of all columns of my df. Important for me is to get also the rownames of these values. Here a subset of my data:

structure(list(BLUE.fruits = c(12803543, 3745797, 19947613, 0, 130, 4), 
BLUE.nuts = c(21563867, 533665, 171984, 0, 0, 0),
BLUE.veggies = c(92690, 188940, 34910, 0, 0, 577),
GREEN.fruits = c(3389314, 15773576, 8942278, 0, 814, 87538),
GREEN.nuts = c(6399474, 1640804, 464688, 0, 0, 0), 
GREEN.veggies = c(15508, 174504, 149581, 0, 0, 6190),
GREY.fruits = c(293869, 0, 188368, 0, 8, 0),
GREY.nuts = c(852646, 144024, 26592, 0, 0, 0), 
GREY.veggies = c(2992, 41267, 6172, 0, 0, 0)),
.Names = c("BLUE.fruits", "BLUE.nuts", "BLUE.veggies", 
"GREEN.fruits", "GREEN.nuts", "GREEN.veggies", "GREY.fruits", 
"GREY.nuts", "GREY.veggies"), row.names = c("Afghanistan", "Albania", 
"Algeria", "American Samoa", "Angola", "Antigua and Barbuda"),
class = "data.frame")

I tried this so far for the first column:

as.data.frame(x[,1][order(x[,1], decreasing=TRUE)][1:10]

However, I don't get the original rownames and I need an approach as apply/lapply to go through all columns (~ 150 cols). Ideas? Thanks

Developer
  • 917
  • 2
  • 9
  • 25
N.Varela
  • 910
  • 1
  • 11
  • 25
  • Have a look at this post http://stackoverflow.com/questions/24212739/how-to-find-the-highest-value-of-a-column-in-a-data-frame-in-r – Developer Nov 27 '15 at 22:20

2 Answers2

1

This could help: Print one column of data frame with row names

So if you adapt your code a bit you get: (A long ugly code line =) , that returns a list, what your desired output format is - based on your "lapply" tag?)

lapply(1:dim(df)[2], function(col.number) df[order(df[, col.number], decreasing=TRUE)[1:3], col.number, drop = FALSE])
Community
  • 1
  • 1
Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
0

You could write a column maximum function, colMax.

colMax <- function(data) sapply(data, max, na.rm = TRUE)
Al V
  • 1,227
  • 2
  • 11
  • 15