-2

I have a sequence and then times that are recorded within each sequence. I am trying to find the max value of time that is recorded with its corresponding sequence. Example below:

    Seq    seconds
1    1       1
2    1       2
3    1       3
4    1       4
5    2       1
6    2       2 
7    3       1
8    3       2
9    3       3
10   3       4
11   3       5

I would like a result that tells me the max time that was recorded in each sequence.

Seq   Time
 1     4
 2     2
 3     5

2 Answers2

1

A solution from dplyr.

library(dplyr)
dt2 <- dt %>%
  arrange(Seq, seconds) %>%
  group_by(Seq) %>%
  slice(n())
dt2
# A tibble: 3 x 2
# Groups:   Seq [3]
    Seq seconds
  <int>   <int>
1     1       4
2     2       2
3     3       5

DATA

dt <- read.table(text = "    Seq    seconds
1    1       1
                 2    1       2
                 3    1       3
                 4    1       4
                 5    2       1
                 6    2       2 
                 7    3       1
                 8    3       2
                 9    3       3
                 10   3       4
                 11   3       5",
                 header = TRUE)
www
  • 35,154
  • 12
  • 33
  • 61
1

An option using data.table

library(data.table)
setDT(df1)[, .(Time = max(seconds)), Seq]
#   Seq Time
#1:   1    4
#2:   2    2
#3:   3    5
akrun
  • 674,427
  • 24
  • 381
  • 486