在R中更改日期格式 [英] Changing date format in R

查看:261
本文介绍了在R中更改日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有一些非常简单的数据,需要更改日期格式:

 日期中点
1 31/08/2011 0.8378
2 31/07/2011 0.8457
3 30/06/2011 0.8147
4 31/05/2011 0.7970
5 30/04/2011 0.7877
31 31/03/2011 0.7411
7 28/02/2011 0.7624
8 31/01/2011 0.7665
9 31/12/2010 0.7500
10 30/11/2010 0.7734
31 31/10/2010 0.7511
12 30/09/2010 0.7263
13 31/08/2010 0.7158
14 31/07/2010 0.7110
15 30/06/2010 0.6921
16 31/05/2010 0.7005
17 30/04/2010 0.7113
18 31/03/2010 0.7027
19 28 / 02/2010 0.6973
20 31/01/2010 0.7260
21 31/12/2009 0.7154
22 30/11/2009 0.7287
23 31/10/2009 0.7375

而不是%d /%m /%Y ,我想用标准R格式%Y-%m-%d



我做这个改变?我已经尝试过:

$ p $ nzd $ date< - format(as.Date(nzd $ date),%Y /%但是,这只是切断了一年,并增加了一天的零:

$> b
$ b

/ p>

  [1]0031/08/200031/07/200030/06/200031 / 05/200030/04/20
[6]0031/03/200028/02/200031/01/200031/12/200030/11 / 20
[11]0031/10/200030/09/200031/08/200031/07/200030/06/20
[ 16]0031/05/200030/04/200031/03/200028/02/200031/01/20
[21]0031/12/20 0030/11/200031/10/200030/09/200031/08/20
[26]0031/07/200030/06/20 0031/05/200030/04/200031/03/20
[31]0028/02/200031/01/200031/12/20 0030/11/200031/10/20
[36]0030/09/200031/08/200031/07/200030/06/200031 / 05/20

感谢!

解决方案

这里有两个步骤:


  • 解析数据。你的例子不是完全可重复的,是文件中的数据,还是文本或因子变量中的变量?让我们假设后者,那么如果你的data.frame被称为X,你可以做


    lockquote
    $ $ p $ ($ X $ date),%d /%m /%Y)
    newdate 列应该是类型的


    日期




    • 格式化数据。这是调用 format() strftime()




      •  格式(X $ newdate,%Y-%m-%d)


        更完整的例子:

          R> nzd < -  data.frame(date = c(31/08/2011,31/07/2011,30/06/2011),
        + mid = c(0.8378,0.8457, 0.8147))
        R> nzd
        日期中
        1 31/08/2011 0.8378
        2 31/07/2011 0.8457
        3 30/06/2011 0.8147
        R> nzd $ newdate< - strptime(as.character(nzd $ date),%d /%m /%Y)
        R> nzd $ txtdate< - format(nzd $ newdate,%Y-%m-%d)
        R> nzd
        日期中值新值txtdate
        1 31/08/2011 0.8378 2011-08-31 2011-08-31
        2 31/07/2011 0.8457 2011-07-31 2011-07- 31
        3 30/06/2011 0.8147 2011-06-30 2011-06-30
        R>

        第三列和第四列的区别在于: newdate 是类日期,而 txtdate 是字符。


        I have some very simple data in R that needs to have its date format changed:

         date midpoint
        1   31/08/2011   0.8378
        2   31/07/2011   0.8457
        3   30/06/2011   0.8147
        4   31/05/2011   0.7970
        5   30/04/2011   0.7877
        6   31/03/2011   0.7411
        7   28/02/2011   0.7624
        8   31/01/2011   0.7665
        9   31/12/2010   0.7500
        10  30/11/2010   0.7734
        11  31/10/2010   0.7511
        12  30/09/2010   0.7263
        13  31/08/2010   0.7158
        14  31/07/2010   0.7110
        15  30/06/2010   0.6921
        16  31/05/2010   0.7005
        17  30/04/2010   0.7113
        18  31/03/2010   0.7027
        19  28/02/2010   0.6973
        20  31/01/2010   0.7260
        21  31/12/2009   0.7154
        22  30/11/2009   0.7287
        23  31/10/2009   0.7375
        

        Rather than %d/%m/%Y, I would like it in the standard R format of %Y-%m-%d

        How can I make this change? I have tried:

        nzd$date <- format(as.Date(nzd$date), "%Y/%m/%d")
        

        But that just cut off the year and added zeros to the day:

         [1] "0031/08/20" "0031/07/20" "0030/06/20" "0031/05/20" "0030/04/20"
         [6] "0031/03/20" "0028/02/20" "0031/01/20" "0031/12/20" "0030/11/20"
         [11] "0031/10/20" "0030/09/20" "0031/08/20" "0031/07/20" "0030/06/20"
         [16] "0031/05/20" "0030/04/20" "0031/03/20" "0028/02/20" "0031/01/20"
         [21] "0031/12/20" "0030/11/20" "0031/10/20" "0030/09/20" "0031/08/20"
         [26] "0031/07/20" "0030/06/20" "0031/05/20" "0030/04/20" "0031/03/20"
         [31] "0028/02/20" "0031/01/20" "0031/12/20" "0030/11/20" "0031/10/20"
         [36] "0030/09/20" "0031/08/20" "0031/07/20" "0030/06/20" "0031/05/20"
        

        Thanks!

        解决方案

        There are two steps here:

        • Parse the data. Your example is not fully reproducible, is the data in a file, or the variable in a text or factor variable? Let us assume the latter, then if you data.frame is called X, you can do

         X$newdate <- strptime(as.character(X$date), "%d/%m/%Y")
        

        Now the newdate column should be of type Date.

        • Format the data. That is a matter of calling format() or strftime():

         format(X$newdate, "%Y-%m-%d")
        

        A more complete example:

        R> nzd <- data.frame(date=c("31/08/2011", "31/07/2011", "30/06/2011"), 
        +                    mid=c(0.8378,0.8457,0.8147))
        R> nzd
                date    mid
        1 31/08/2011 0.8378
        2 31/07/2011 0.8457
        3 30/06/2011 0.8147
        R> nzd$newdate <- strptime(as.character(nzd$date), "%d/%m/%Y")
        R> nzd$txtdate <- format(nzd$newdate, "%Y-%m-%d")
        R> nzd
                date    mid    newdate    txtdate
        1 31/08/2011 0.8378 2011-08-31 2011-08-31
        2 31/07/2011 0.8457 2011-07-31 2011-07-31
        3 30/06/2011 0.8147 2011-06-30 2011-06-30
        R> 
        

        The difference between columns three and four is the type: newdate is of class Date whereas txtdate is character.

        这篇关于在R中更改日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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