将"7-2014"转换为日期格式并在R中找到最小值 [英] converting `"7-2014"`into date format and finding minimum in R

查看:98
本文介绍了将"7-2014"转换为日期格式并在R中找到最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在矢量date

> head(date)
[1] "7-2014"  "1-2018"  "11-2014" "7-2014"  "1-2018"  "1-2018" 

,并希望将其转换为日期并找到最小值.我按照此处

and want to convert it to date and find its minimum. I used as.Date as explained here

as.Date(date, "%m-%Y")

,但仅得到NA作为结果.为什么这不起作用?

but only get NA as result. Why is this not working?

我想在该栏中找到最短日期.如果您知道更好的方法,请告诉我:)

I want to find the minimum date in the column. If you know of a better approach, enlighten me :)

推荐答案

按日期需要一个月中的某天:

as.Date requires a day of the month:

date <- c("7-2014",  "1-2018",  "11-2014", "7-2014",  "1-2018",  "1-2018")
as.Date(paste0("1-", date), "%d-%m-%Y")
#> [1] "2014-07-01" "2018-01-01" "2014-11-01" "2014-07-01" "2018-01-01"
#> [6] "2018-01-01"

reprex软件包(v0.3.0)创建于2020-04-15 sup>

Created on 2020-04-15 by the reprex package (v0.3.0)

这篇关于将"7-2014"转换为日期格式并在R中找到最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆