Home > Blockchain >  How to return a value of a column that is associated with the max(date) in R?
How to return a value of a column that is associated with the max(date) in R?

Time:12-10

I am seeking an answer to finding a value that is tied to the max date which is also tied to an id value in R. The dataframe looks like

id value date
1 A 12/12/2021
1 B 12/13/2021
1 A 12/14/2021
2 A 12/13/2021
2 C 12/07/2021
2 B 12/17/2021
3 C 12/13/2021
3 B 12/06/2021
3 C 12/02/2021

The code should return:

id value date max_value
1 A 12/12/2021 A
1 B 12/13/2021 A
1 A 12/14/2021 A
2 A 12/13/2021 B
2 C 12/07/2021 B
2 B 12/17/2021 B
3 C 12/13/2021 C
3 B 12/06/2021 C
3 C 12/02/2021 C

I have tried the following & get an error.

df <- df[!is.na(df$date),]
    for(ID in unique(df$id)){
      as.data.frame(df %>% filter(id == ID) %>% dplyr::mutate(max_value = ifelse(df$date == max(df$date, na.rm = T), df$value, df$value[df$date == max(df$date, na.rm = T) & df$id == ID])))
    }

CodePudding user response:

Try the following dplyr approach:

df %>% 
  group_by(id) %>%
  mutate(max = value[date == max(date)])

Output:

#      id value date       max  
# <int> <chr> <chr>      <chr>
# 1     1 A     12/12/2021 A    
# 2     1 B     12/13/2021 A    
# 3     1 A     12/14/2021 A    
# 4     2 A     12/13/2021 B    
# 5     2 C     12/07/2021 B    
# 6     2 B     12/17/2021 B    
# 7     3 C     12/13/2021 C    
# 8     3 B     12/06/2021 C    
# 9     3 C     12/02/2021 C 
  • Related