Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
733 views
in Technique[技术] by (71.8m points)

date - Find out the number of days of a month in R

I have a date in P

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

lubridate package has required function "days_in_month" with respect to leapyears.

Your example:

    date <- as.Date("2011-02-23", "%Y-%m-%d")
    lubridate::days_in_month(date)
    # Feb
    # 28

Leap year (2016):

    date_leap <- as.Date("2016-02-23", "%Y-%m-%d")
    lubridate::days_in_month(date_leap)
    # Feb 
    # 29

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...