Skip to content

How should month() behave when given an integer vector? #887

@jameslairdsmith

Description

@jameslairdsmith

month() has a (currently undocumented) feature where it can convert a numeric vector into an ordered factor of month names or abbreviations. If given invalid input, however, it produces undesirable results. If label = T then the input values that are not integers from 1 to 12 return <NA> instead triggering an error. This might make someone think the value in question was missing rather than invalid. Not sure why anyone would do this, but if label = F however, then the input values are just returned as output.

Might be worth having a separate function to perform this operation, perhaps something like fct_month()?

library(lubridate, warn.conflicts = F)

month(1:3, label = T)
#> [1] Jan Feb Mar
#> 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
month(0:3, label = T)
#> [1] <NA> Jan  Feb  Mar 
#> 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
month(1:3)
#> [1] 1 2 3
month(0:3)
#> [1] 0 1 2 3

Created on 2020-04-25 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions