0

I want to get the max dated person-country in R

enter image description here

I want to get the country of the person of his max date as table like that :

enter image description here

determin
  • 69
  • 7

2 Answers2

0

Here is one option. After grouping by 'person', convert the 'date' to Date class with lubridate (assuming that the 'date' format is in %d.%m.%Y), get the index of the row with which.max and slice that row

library(dplyr)
library(lubridate)
df1 %>%
   group_by(person) %>%
   slice(which.max(dmy(date)))
   # if the format is %m.%d.%Y"
   # slice(which.max(mdy(date)))
# A tibble: 2 x 3
# Groups:   person [2]
#  person country date      
#  <chr>  <chr>   <chr>     
#1 a      germany 01.05.2020
#2 c      korea   01.01.2023

Or using data.table

library(data.table)
setDT(df1)[, .SD[which.max(as.IDate(date, "%d.%m.%Y"))], person]

data

df1 <- structure(list(person = c("a", "a", "c", "c", "c"), country = c("usa", 
"germany", "france", "china", "korea"), date = c("01.01.2020", 
"01.05.2020", "01.01.2021", "01.01.2022", "01.01.2023")),
class = "data.frame", row.names = c(NA, 
-5L))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

In base R, we can first transform into Date object and then select max date for each person.

subset(transform(df, date = as.Date(date, "%d.%m.%Y")), 
                 date == ave(date, person, FUN = max))

#  person country       date
#2      a germany 2020-05-01
#5      c   korea 2023-01-01
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213