找出R中一个月的天数 [英] Find out the number of days of a month in R

查看:190
本文介绍了找出R中一个月的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期在P

 date = as.Date("2011-02-23", "%Y-%m-%d")

是否可以找出当月的天数那个特定日期? (关于leapyears)。在PHP中,它看起来与此类似( http://www.php.net/ manual / en / function.date.php ):

Is it possible to find out the number of days of the month of that particular date? (With respect to leapyears). In PHP it would look similar to this (http://www.php.net/manual/en/function.date.php):

days = format(date, "%t")

但%t似乎在R中有不同的含义。有没有解决这个问题?

but "%t" seems to have a different meaning in R. Is there a solution for this problem?

推荐答案

您可以编写简单的功能:

You can write simple function to do that:

numberOfDays <- function(date) {
    m <- format(date, format="%m")

    while (format(date, format="%m") == m) {
        date <- date + 1
    }

    return(as.integer(format(date - 1, format="%d")))
}

调用为:

> date = as.Date("2011-02-23", "%Y-%m-%d")
> numberOfDays(date)
[1] 28
> date # date is unchanged
[1] "2011-02-23"

这篇关于找出R中一个月的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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