r - R获取最大日期字符串
解决方案
这是一种选择。按'person'分组后,将'date'转换为Date
class with lubridate
(假设'date'格式为in %d.%m.%Y
),得到行withwhich.max
和slice
该行的索引
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
或使用data.table
library(data.table)
setDT(df1)[, .SD[which.max(as.IDate(date, "%d.%m.%Y"))], person]
数据
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))
推荐阅读
- fullcalendar - Fullcalendar 自定义月份从 24 日开始,到 23 日结束
- c# - Url.Action 方法返回错误的 url 字符串
- java - 如何创建循环枚举定义?
- angular - 使用 Angular Universal 和 Ionic App 时未定义窗口错误
- c++ - 如何从命令行编译/使用 MSVC 中的标头单元?
- python - Keras:使用自动编码器进行预测
- angular - 如何使用 ActivatedRoute 测试我的组件?
- r - 在 R 中读取包含非 ASCII 字符的文件
- r - 无法安装 magrittr
- json - 在 Ruby 中的单个对象中保存多个 api 响应