简体   繁体   中英

Select row with most recent date by group in R from sql

I want to import data from SQL server into R. I have multiple entry ID for different Dates. I need only active observations (ie 'endDate'<'lastUpdate') and for other ones I need only most recent observation for each ID. I wright following code:

df <- dt %>% 
      group_by(ID) %>%
      slice(which.max(as.Date(date, '%m/%d/%Y'))) %>%
      select(1:13) %>%
      collect()

where dt=tbl(con, database) though I get following error:

"Error: slice() is not suppoted on database backends"

how can handle this problem: how import data in such stucture?

For such tasks it is better to use filter

dt %>% 
      group_by(ID) %>%
      filter(max(date) == date)
      ...

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