简体   繁体   English

用非标准格式格式化日期

[英]Formatting Dates with non-standard format

I'm relatively new to this site so forgive me if my question is a bit vague for you guys. 我是这个网站的新手,所以如果我的问题对你们来说有点模糊,请原谅我。 I also realize there are many threads on this topic, yet I feel they do not answer my question specifically since they are almost all about changing yy/mm/dd to dd/mm/yy or vice versa. 我也意识到这个主题有很多话题,但是我觉得他们没有具体回答我的问题,因为它们几乎都是关于将yy / mm / dd更改为dd / mm / yy,反之亦然。

In short what do i want? 简而言之,我想要什么? I want my current format changed into only year. 我希望将当前格式更改为仅一年。

I have a column full of dates of this format. 我有一栏充满这种格式的日期。

31OCT2016:23:52:00.000

I've seen in many topics you can use format commands but they go something like this; 我在很多主题中都可以使用格式命令,但是它们却像这样。

dates <- c("05/27/84", "07/07/05")

I have over 100.000 observations so this can't be done manually. 我有超过100.000个观察结果,因此无法手动完成。 So I tried; 所以我尝试了;

mydata$dates <- format(as.Date(mydata$dates), "%Y")

But that didn't work. 但这没有用。 I saw on this website the proper values 我在这个网站上看到了正确的价值观

http://www.statmethods.net/input/dates.html http://www.statmethods.net/input/dates.html

But it did not say anything on how to get rid of hours minutes and seconds. 但是它没有说明如何摆脱数小时和数秒的时间。

So what is the easiest way to strip it all down to year only? 那么,将其剥离到一年以下的最简单方法是什么?

Lubridate is your friend. Lubridate是您的朋友。 To be precise, the function dmy_hms : 确切地说,函数dmy_hms

I'll generate some sample data which has the same format as your example so my code is reproducible. 我将生成一些示例数据,其格式与您的示例相同,因此我的代码可重复。 Don't worry about it too much. 不用太担心。 For your purposes, you can jump right to the conversion part. 为了您的目的,您可以直接跳到转换部分。

#------------------------------------------------------------------------------------------
#This code block is entirely for generating reproducible sample data

d <- sample(1:27,10,T)
mon <- toupper(sample(month.abb,10,T))
y <- sample(2000:2017,10,T)
h <- sample(0:23,10,T)
min <- sample(0:59,10,T)
s <- sample(0:59,10,T)

#load package
library(lubridate)

dts <- sprintf('%02d%s%s:%s:%s:%s.000',d,mon,y,h,min,s)

> dts
 [1] "01JAN2012:12:6:53.000"  "01NOV2010:0:19:47.000"  "03SEP2000:9:45:3.000"   "25NOV2009:21:39:57.000" "08DEC2015:19:27:36.000"
 [6] "23MAR2009:13:39:40.000" "03JUN2010:14:54:50.000" "03APR2002:6:34:45.000"  "19NOV2012:5:17:29.000"  "02FEB2003:0:3:59.000" 

#------------------------------------------------------------------------------------------

So basically the variable dts is your column full of dates which you want to convert: 因此,基本上,变量dts是您要转换的充满日期的列:

#conversion

> dmy_hms(dts)
 [1] "2012-01-01 12:06:53 UTC" "2010-11-01 00:19:47 UTC" "2000-09-03 09:45:03 UTC" "2009-11-25 21:39:57 UTC"
 [5] "2015-12-08 19:27:36 UTC" "2009-03-23 13:39:40 UTC" "2010-06-03 14:54:50 UTC" "2002-04-03 06:34:45 UTC"
 [9] "2012-11-19 05:17:29 UTC" "2003-02-02 00:03:59 UTC"

And then to get just the years, you can use the year function: 然后,要获得年份,可以使用year函数:

> year(dmy_hms(dts))
 [1] 2012 2010 2000 2009 2015 2009 2010 2002 2012 2003

So assuming you want to do everything inside the data.frame , your code could look like this: 因此,假设您想在data.frame进行所有data.frame ,您的代码应如下所示:

# example dataframe
dframe <- data.frame(variable=c('A','B','C'),dates=sample(dts,3))

This is a data frame with some variable and the column with the dates. 这是带有某些变量的数据框,而带有日期的列。

> dframe
  variable                 dates
1        A  15JAN2000:0:37:6.000
2        B 13DEC2016:8:34:28.000
3        C 18AUG2005:2:27:16.000

So to convert the dates, we can simply do dframe$dates <- year(dmy_hms(dframe$dates)) 因此,要转换日期,我们可以简单地执行dframe$dates <- year(dmy_hms(dframe$dates))

If we look at dframe again, we can see that the conversion was successful: 如果再次查看dframe ,我们可以看到转换成功:

> dframe
  variable dates
1        A  2000
2        B  2016
3        C  2005

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM