dplyr - Get last value for each year

后端 未结 1 1064
孤独总比滥情好
孤独总比滥情好 2021-02-05 11:16

I have a tbl_df that looks like this:

> d
Source: local data frame [3,703 x 3]

         date  value year
1  2001-01-01 0.1218 2001
2  2001-01-02 0.1216 2001         


        
相关标签:
1条回答
  • 2021-02-05 11:43

    Here are some options

    library(dplyr)
    d %>% 
      group_by(year) %>%
      summarise(value=last(value))
    

    Or may be (not very clear in the description)

    d %>% 
      group_by(year) %>%
      slice(which.max(date)) %>%
      select(value) 
    

    Or

    d %>%
      group_by(year) %>%
      filter(date==max(date)) %>%
      select(value)
    

    Or we can use arrange to order the 'date' (in case it is not ordered) and get the last value

    d %>%
      group_by(year) %>%
      arrange(date) %>%
      summarise(value=last(value))
    

    In case, you want to try with data.table, here is one

    library(data.table)
    setDT(d)[, value[which.max(date)], year]
    

    Or as @David Arenburg commented

     unique(setDT(d)[order(-date)], by = "year")
    
    0 讨论(0)
提交回复
热议问题