user2946746
user2946746

Reputation: 1780

Number of days in the month in R?

I'm trying to get the number days in the given month. But I can not figure out how to strip out just the number portion from the "days_in_month" function.

require(lubridate) 
st <- as.Date("1998-12-17")
en <- as.Date("2000-1-7")
ll <- seq(en, st, by = "-1 month")
t<-days_in_month(ll)

This is returning:

 Jan Dec Nov Oct Sep Aug Jul Jun May Apr Mar Feb Jan 
 31  31  30  31  30  31  31  30  31  30  31  28  31 

I do not want the Jan.... How to do go about just getting the vector of numbers?

Thank you

Upvotes: 2

Views: 1898

Answers (1)

tim
tim

Reputation: 3608

If you str(t), you see that what you've generated is a list of integers, with a names attribute.

Named int [1:13] 31 31 30 31 30 31 31 30 31 30 ...
- attr(*, "names")= chr [1:13] "Jan" "Dec" "Nov" "Oct" ...

To remove the attributes, just

as.numeric(t)
# [1] 31 31 30 31 30 31 31 30 31 30 31 28 31

Or, more efficiently strip the names

unname(t)
[1] 31 31 30 31 30 31 31 30 31 30 31 28 31

or render as vector (both suggested above by @akrun

as.vector(t)

[1] 31 31 30 31 30 31 31 30 31 30 31 28 31

Also, as @thelatemail notes, this often is irrelevant, as functions wanting the numbers will do this stripping for you (though sometimes the attributes can cause errors).

Upvotes: 1

Related Questions