英文:
Is there a way to select one string value that contains the latest date relative to the values in the same list
问题
我希望保留以下部分:
Latest <- "4-3-23 is actually the latest"
英文:
I have a vector like this
c("The date is 1-2-23","Here it is 1-16-23",
"Here 2-16-23 is the latest",
"4-3-23 is actually the latest","12-12-22 is the earliest")
I would like to be left with
Latest<-"4-3-23 is actually the latest"
答案1
得分: 3
这是一个用于提取、排序和获取最新日期的管道:
library(dplyr)
library(stringr)
data.frame(strings = c("日期是 1-2-23","这里是 1-16-23",
"这里 2-16-23 是最新的",
"4-3-23 实际上是最新的","12-12-22 是最早的")) %>%
mutate(date = as.Date(str_extract(strings, "(\\d+-?){3}"), "%m-%d-%y")) %>%
arrange(desc(date)) %>%
slice(1) %>%
pull(strings)
英文:
Here is a pipeline to extract, sort, and pull the most recent:
library(dplyr)
library(stringr)
data.frame(strings = c("The date is 1-2-23","Here it is 1-16-23",
"Here 2-16-23 is the latest",
"4-3-23 is actually the latest","12-12-22 is the earliest")) %>%
mutate(date = as.Date(str_extract(strings, "(\\d+-?){3}"), "%m-%d-%y")) %>%
arrange(desc(date)) %>%
slice(1) %>%
pull(strings)
答案2
得分: 3
以下是代码部分的翻译:
The most simple way to parse the dates seems to be mdy()
from the lubridate package which is now part of the Tidyverse:
library(tidyverse)
x <- c(
"The date is 1-2-23",
"Here it is 1-16-23",
"Here 2-16-23 is the latest",
"4-3-23 is actually the latest",
"12-12-22 is the earliest"
)
x[which.max(mdy(x))]
#> [1] "4-3-23 is actually the latest"
Created on 2023-04-10 with reprex v2.0.2
英文:
The most simple way to parse the dates seems to be mdy()
from the lubridate package which is now part of the Tidyverse:
library(tidyverse)
x <- c(
"The date is 1-2-23",
"Here it is 1-16-23",
"Here 2-16-23 is the latest",
"4-3-23 is actually the latest",
"12-12-22 is the earliest"
)
x[which.max(mdy(x))]
#> [1] "4-3-23 is actually the latest"
<sup>Created on 2023-04-10 with reprex v2.0.2</sup>
答案3
得分: 2
更新: 需要使用日期类(感谢 @thelatemail):
max(as.Date(regmatches(vec, regexpr("\\d{1,2}-\\d{1,2}-\\d{2}", vec)), "%m-%d-%y"))
[1] "2023-04-03"
第一个答案:
我们可以这样做:
- 创建匹配日期格式(mm-dd-yy)的正则表达式
- 使用
regmatches
和regexpr
提取日期 - 使用
max
找到最新日期 - 使用
paste
创建字符串
paste(max(regmatches(vec, regexpr("\\d{1,2}-\\d{1,2}-\\d{2}", vec))), "实际上是最新的")
"4-3-23实际上是最新的"
英文:
Update: with have to use class date (thanks to @thelatemail):
max(as.Date(regmatches(vec, regexpr("\\d{1,2}-\\d{1,2}-\\d{2}", vec)), "%m-%d-%y"))
[1] "2023-04-03"
First answer:
We could do it this way:
- Create regex to match date with format (mm-dd-yy)
- with
regmatches
andregexpr
extract the date - use
max
for latest date - use
paste
for string
paste(max(regmatches(vec, regexpr("\\d{1,2}-\\d{1,2}-\\d{2}", vec))), "is actually the latest")
"4-3-23 is actually the latest"
答案4
得分: 2
Parsing as.Date
and ordering after getting the date string with sub
stri[order(as.Date(
sub(".*(\\d+-\\d+-\\d+).*", "\", stri), "%m-%d-%y"), decreasing=T)[1]]
[1] "4-3-23 is actually the latest"
Data
stri <- c("The date is 1-2-23","Here it is 1-16-23",
"Here 2-16-23 is the latest",
"4-3-23 is actually the latest","12-12-22 is the earliest")
英文:
Parsing as.Date
and order
ing after getting the date string with sub
stri[order(as.Date(
sub(".*(\\d+-\\d+-\\d+).*", "\", stri), "%m-%d-%y"), decreasing=T)[1]]
[1] "4-3-23 is actually the latest"
Data
stri <- c("The date is 1-2-23","Here it is 1-16-23",
"Here 2-16-23 is the latest",
"4-3-23 is actually the latest","12-12-22 is the earliest")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论