-2

As title suggested , I have a query to access the row value (not the index and not the entire row) corresponding to max value of a column.

FOR EX: I have below data set(results):

Name         subject      marks
sam           maths        88
paul          science      79
tom           social       66
james         english      78
richard       maths        83

Need help for following:

  1. How to write command to retrieve row value ("sam") corresponding to max col value of marks (88)?

I was able to get the max value of marks column using the below command:

max(results$marks, na.rm = TRUE)

However, when I assign it to a variable to see who topped the exam, I get only the header names no the value as sam.

top_scorer<- results$Name[which.max(results$marks, na.rm = TRUEr)]
  1. How to write a command to find out which person comes second on the list in terms of marks?

Please help. I am beginner in R programming.

Uwe
  • 41,420
  • 11
  • 90
  • 134
R Learner
  • 11
  • 1
  • Possible duplicate of [How to select the row with the maximum value in each group](http://stackoverflow.com/questions/24558328/how-to-select-the-row-with-the-maximum-value-in-each-group) – C8H10N4O2 Apr 09 '17 at 01:16

1 Answers1

2

You're almost there. You actually use the which.max in your example code. To get the person who has the second highest marks you can order it by marks and take the second entry. This should work for both. We use which.max for the maximum and the sorting for the second maximum:

df <- data.frame(Name=c("sam","paul","tom","james", "richard"), 
                 subject=c("maths", "science", "social", "english", "maths"),
                 marks=c(88,79, 66, 78, 83))


df$max_name <- df$Name[which.max(df$marks)]
df <- df[order(df$marks,decreasing=T),]
df$second_max_name <- df$Name[2]

Note that you could use the sorting to get the max as well if you want, the order function defaults to putting NAs last.

Mike H.
  • 13,960
  • 2
  • 29
  • 39