Is there a way to select one string value that contains the latest date relative to the values in the same list

huangapple go评论110阅读模式
英文:

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(&quot;The date is 1-2-23&quot;,&quot;Here it is 1-16-23&quot;,
&quot;Here 2-16-23 is the latest&quot;, 
&quot;4-3-23 is actually the latest&quot;,&quot;12-12-22 is the earliest&quot;)

I would like to be left with

Latest&lt;-&quot;4-3-23 is actually the latest&quot;

答案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(&quot;The date is 1-2-23&quot;,&quot;Here it is 1-16-23&quot;,
                       &quot;Here 2-16-23 is the latest&quot;, 
                       &quot;4-3-23 is actually the latest&quot;,&quot;12-12-22 is the earliest&quot;)) %&gt;% 
  mutate(date = as.Date(str_extract(strings, &quot;(\\d+-?){3}&quot;), &quot;%m-%d-%y&quot;)) %&gt;% 
  arrange(desc(date)) %&gt;% 
  slice(1) %&gt;% 
  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 &lt;- c(
  &quot;The date is 1-2-23&quot;,
  &quot;Here it is 1-16-23&quot;,
  &quot;Here 2-16-23 is the latest&quot;,
  &quot;4-3-23 is actually the latest&quot;,
  &quot;12-12-22 is the earliest&quot;
)

x[which.max(mdy(x))]
#&gt; [1] &quot;4-3-23 is actually the latest&quot;

<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"

第一个答案:
我们可以这样做:

  1. 创建匹配日期格式(mm-dd-yy)的正则表达式
  2. 使用 regmatchesregexpr 提取日期
  3. 使用 max 找到最新日期
  4. 使用 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(&quot;\\d{1,2}-\\d{1,2}-\\d{2}&quot;, vec)), &quot;%m-%d-%y&quot;))

[1] &quot;2023-04-03&quot;

First answer:
We could do it this way:

  1. Create regex to match date with format (mm-dd-yy)
  2. with regmatches and regexpr extract the date
  3. use max for latest date
  4. use paste for string
paste(max(regmatches(vec, regexpr(&quot;\\d{1,2}-\\d{1,2}-\\d{2}&quot;, vec))), &quot;is actually the latest&quot;)

&quot;4-3-23 is actually the latest&quot;

答案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 ordering after getting the date string with sub

stri[order(as.Date(
   sub(&quot;.*(\\d+-\\d+-\\d+).*&quot;, &quot;\&quot;, stri), &quot;%m-%d-%y&quot;), decreasing=T)[1]]
[1] &quot;4-3-23 is actually the latest&quot;

Data

stri &lt;- c(&quot;The date is 1-2-23&quot;,&quot;Here it is 1-16-23&quot;,
&quot;Here 2-16-23 is the latest&quot;, 
&quot;4-3-23 is actually the latest&quot;,&quot;12-12-22 is the earliest&quot;)

huangapple
  • 本文由 发表于 2023年4月11日 04:39:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定