-4

I was working on the Faithful built in data set in R and I wanted to find out the interval which had the maximum number of eruptions. I already grouped the data into intervals and using the max() function I get the value of the max eruptions but not the interval. Is there anyway to display the interval which has the maximum number of eruptions?

The table is given below:

[1.5,2)      51
[2,2.5)      41
[2.5,3)       5
[3,3.5)       7
[3.5,4)      30
[4,4.5)      73
[4.5,5)      61
[5,5.5)       4

Considering here, the max is 73, how do I print [4, 4.5) ?

Aramis7d
  • 2,444
  • 19
  • 25
Amit Naik
  • 121
  • 2
  • 17
  • 3
    what is the code you've used to create this table? – Spätzle Jan 08 '17 at 05:20
  • @Amit Naik You should provide a reproducible example http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – shiny Jan 08 '17 at 05:22
  • Your data is hard to reproduce. Something like this may work `d <- c(1,3,2,2,1) which(d == max(d))` – Andrew Lavers Jan 08 '17 at 05:33
  • `names(which.max(table(cut(faithful$eruptions, seq(1.5, 5.5, .5), right = FALSE))))` – alistaire Jan 08 '17 at 05:34
  • I first found out the range using 'range(faithful$eruptions)' . Then I divided the range into sub-intervals using the 'seq()' function. Then I classified the data into the sub-intervals using the 'cut()' function, which I put in a table. – Amit Naik Jan 08 '17 at 06:11

1 Answers1

1

If your data is in a named num q like this:

> q
[1.5,2) [2,2.5) [2.5,3) [3,3.5) [3.5,4) [4,4.5) [4.5,5) [5,5.5) 
     51      41       5       7      30      73      61       4

use names:

> names(q)[which.max(q)]
[1] "[4,4.5)"

If it's in any kind of dataframe/matrix/t p, use rownames:

> p
        max_er
[1.5,2)     51
[2,2.5)     41
[2.5,3)      5
[3,3.5)      7
[3.5,4)     30
[4,4.5)     73
[4.5,5)     61
[5,5.5)      4

> rownames(p)[which.max(p$max_er)]
[1] "[4,4.5)"
Spätzle
  • 709
  • 10
  • 20
  • 1
    @AmitNaik - You could also respond to all the comments under your question, especially those about providing a reproducible example – Rich Scriven Jan 08 '17 at 05:47