Emil Lykke Jensen
Emil Lykke Jensen

Reputation: 409

Finding the row before maximum value using R

I'm interested in finding the row before the max value in R. The dataframe looks like this:

    timeround   n
1   01:00   11
2   02:00   6
3   03:00   4
4   04:00   4
5   05:00   7
6   06:00   22
7   07:00   63
8   08:00   142
9   09:00   155
10  10:00   220
11  11:00   143
12  12:00   98
13  13:00   111
14  14:00   115
15  15:00   129
16  16:00   128
17  17:00   102
18  18:00   90
19  19:00   108
20  20:00   92
21  21:00   90
22  22:00   102
23  23:00   44
24  24:00   20

Currently I'm able to show the max value, using:

with(tweetcount, timeround[n== max(n)])

giving me the value "10:00"

How do I compute R to give me the value before the max value - in this case "9:00"?

Upvotes: 3

Views: 567

Answers (1)

akrun
akrun

Reputation: 887118

You could use which

with(tweetcount, timeround[which(n== max(n))-1])

If there is only a single max value

 tweetcount$timeround[which.max(tweetcount$n)-1]

Upvotes: 2

Related Questions