Antonios
Antonios

Reputation: 1939

Ranking Values of data frame excluding same dates

I have a data frame with Dates and Values:

library(dplyr)
library(lubridate) 

df<-tibble(DateTime=ymd(c("2018-01-01","2018-01-01","2018-01-02","2018-01-02","2018-01-03","2018-01-03")),
              Value=c(5,10,12,3,9,11),Rank=rep(0,6))

I would like to Rank the values of the two last rows, each compared with the rest four Value rows (the ones of previous dates).

I have managed to do this:

dfReference<-df%>%filter(DateTime!=max(DateTime))

dfTarget<-df%>%filter(DateTime==max(DateTime))

for (i in 1:nrow(dfTarget)){
  tempDf<-rbind(dfReference,dfTarget[i,])%>%
    mutate(Rank=rank(Value,ties.method = "first"))
  dfTarget$Rank[i]=filter(tempDf,DateTime==max(df$DateTime))$Rank
}

Desired output:

> dfTarget
# A tibble: 2 x 3
  DateTime   Value  Rank
  <date>     <dbl> <dbl>
1 2018-01-03     9     3
2 2018-01-03    11     4

But I am looking for a more delicate way.

Thanks

Upvotes: 2

Views: 74

Answers (1)

IceCreamToucan
IceCreamToucan

Reputation: 28695

This is basically the same idea as your for loop, but instead of a loop it uses map_int, and instead of creating a new data frame using rbind it creates a new vector with c().

library(tidyverse)

is.max <- with(df,  DateTime == max(DateTime))

df[is.max,] %>% 
  mutate(Rank = map_int(Value, ~
    c(df$Value[!is.max], .x) %>% 
      rank(ties.method = 'first') %>% 
      tail(1)))



# # A tibble: 2 x 3
#   DateTime   Value  Rank
#   <date>     <dbl> <int>
# 1 2018-01-03     9     3
# 2 2018-01-03    11     4

Upvotes: 3

Related Questions