简体   繁体   中英

Finding the largest year interval in R

Suppose I have 10 years time and name associated to it like following,

Name Year
A    1990
B    1991
C    1992
A    1993
A    1994
.
.
.

I want to find the name that has been out of use for the longest time.

Can anybody help me how to do this?

Using dplyr :

library(dplyr)
mutate(your_data, max_year = max(Year)) %>%
    group_by(Name) %>%
    summarize(most_recent = max(Year),
              unused_length = first(max_year) - most_recent) %>%
    ungroup() %>%
    arrange(most_recent)

This will order the names by their most recent use, with the oldest most recent use first.

If you only care about getting that one most out-of-use name, you just need the first row of the result. Add slice(1) to the chain as so:

mutate(your_data, max_year = max(Year)) %>%
    group_by(Name) %>%
    summarize(most_recent = max(Year),
              unused_length = first(max_year) - most_recent) %>%
    ungroup() %>%
    arrange(most_recent) %>%
    slice(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM