Getting some NA values when using dplyr mutate and lubridate.

I am trying to use dplyr and lubridate to create a new column of my data frame but I am getting some NA values and not sure why. The NA values seem to me to be "random" but there must be some reason why I am getting them since many of the observations contain no NA values.

Code:

  d %>%
    mutate(date_quarter = quarter(date),
           date_quarter_plus_3 = quarter(date + months(3)))

Data:

d <- structure(list(cusip = c("69021210", "50187A10", "77437410", 
"54928210", "23204G10", "78409V10", "68268N10", "22637210", "25400W10", 
"02752P10", "82481R10", "12811R10", "60871R20", "84649R10", "00215F10", 
"52490110", "21868310", "44133910", "00971T10", "86667410", "51070010", 
"57174810", "72383630", "92220710", "14446010", "66611T10", "52077610", 
"83547010", "45798520", "74346Y10", "40424G10", "78430510", "40425J10", 
"57734510", "89678F10", "84863T10", "92342Y10", "76218Y10", "21075N20", 
"78377T10", "87156510", "22100610", "50076Q10", "63633D10", "44840710", 
"G7496G10", "31374720", "40412C10", "71375U10", "87185110", "21075N20", 
"67058H10", "78644920", "50187A10", "86608210", "87874D10", "00095710", 
"65339F10", "61539420", "50207910", "77571110", "56280310", "47012E10", 
"01877R10", "G6331P10", "37247D10", "40412C10", "44267D10", "45778Q10", 
"09180C10", "03763V10", "75936P10", "03073H10", "00476410", "67623R10", 
"57308310", "00770C10", "58353820", "58155Q10", "78129510", "30214U10", 
"42234710", "48242W10", "63938C10", "27000610", "14067E50", "83545R20", 
"91529Y10", "02752P10", "29273R10", "66680710", "49458010", "36269P10", 
"71376K10", "05379B10", "67612W10", "89531P10", "04916420", "58844810", 
"34484910"), date = structure(c(14091, 13511, 15033, 15792, 17225, 
16800, 16860, 15520, 17562, 16708, 17865, 15856, 14334, 14244, 
13938, 16311, 16465, 14274, 16708, 16678, 14974, 15520, 17500, 
14852, 13482, 14943, 16952, 14393, 17470, 14091, 13299, 15460, 
12993, 15825, 16891, 16891, 16860, 16738, 14364, 17590, 13147, 
15583, 15674, 16555, 17044, 14547, 13938, 16738, 17256, 14152, 
13179, 17317, 16220, 15155, 17896, 13299, 17865, 14852, 13909, 
16920, 12842, 16555, 12964, 16525, 15399, 17711, 16616, 15644, 
14091, 16160, 16584, 13087, 14852, 14152, 14183, 14820, 16952, 
13572, 17619, 14638, 16891, 13266, 16220, 16282, 13725, 13969, 
13784, 12993, 16343, 15644, 15370, 13391, 13420, 16038, 14974, 
13238, 15429, 14882, 16435, 17865), class = "Date")), row.names = c(NA, 
-100L), class = "data.frame")

Adding months is a pain because of the irregular numbers of days in the months. Your NAs appear when you generate dates like 2006_06-31. I think the %m+% operator from lubridate will give you want you want.

d <- structure(list(cusip = c("69021210", "50187A10", "77437410", 
                              "54928210", "23204G10", "78409V10", "68268N10", "22637210", "25400W10", 
                              "02752P10", "82481R10", "12811R10", "60871R20", "84649R10", "00215F10", 
                              "52490110", "21868310", "44133910", "00971T10", "86667410", "51070010", 
                              "57174810", "72383630", "92220710", "14446010", "66611T10", "52077610", 
                              "83547010", "45798520", "74346Y10", "40424G10", "78430510", "40425J10", 
                              "57734510", "89678F10", "84863T10", "92342Y10", "76218Y10", "21075N20", 
                              "78377T10", "87156510", "22100610", "50076Q10", "63633D10", "44840710", 
                              "G7496G10", "31374720", "40412C10", "71375U10", "87185110", "21075N20", 
                              "67058H10", "78644920", "50187A10", "86608210", "87874D10", "00095710", 
                              "65339F10", "61539420", "50207910", "77571110", "56280310", "47012E10", 
                              "01877R10", "G6331P10", "37247D10", "40412C10", "44267D10", "45778Q10", 
                              "09180C10", "03763V10", "75936P10", "03073H10", "00476410", "67623R10", 
                              "57308310", "00770C10", "58353820", "58155Q10", "78129510", "30214U10", 
                              "42234710", "48242W10", "63938C10", "27000610", "14067E50", "83545R20", 
                              "91529Y10", "02752P10", "29273R10", "66680710", "49458010", "36269P10", 
                              "71376K10", "05379B10", "67612W10", "89531P10", "04916420", "58844810", 
                              "34484910"), 
                    date = structure(c(14091, 13511, 15033, 15792, 17225, 16800, 16860, 
                                       15520, 17562, 16708, 17865, 15856, 14334, 14244,
                                       13938, 16311, 16465, 14274, 16708, 16678, 14974, 
                                       15520, 17500, 14852, 13482, 14943, 16952, 14393, 
                                       17470, 14091, 13299, 15460, 12993, 15825, 16891, 
                                       16891, 16860, 16738, 14364, 17590, 13147, 15583, 
                                       15674, 16555, 17044, 14547, 13938, 16738, 17256, 
                                       14152, 13179, 17317, 16220, 15155, 17896, 13299, 
                                       17865, 14852, 13909, 16920, 12842, 16555, 12964, 
                                       16525, 15399, 17711, 16616, 15644, 14091, 16160, 
                                       16584, 13087, 14852, 14152, 14183, 14820, 16952, 
                                       13572, 17619, 14638, 16891, 13266, 16220, 16282, 
                                       13725, 13969, 13784, 12993, 16343, 15644, 15370, 
                                       13391, 13420, 16038, 14974, 13238, 15429, 14882, 
                                       16435, 17865), class = "Date")), 
               row.names = c(NA, -100L), class = "data.frame")
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
d %>%
  mutate(date_q = quarter(date),
         date_q_plus_3 = quarter(date + months(3)),
         date_plus_3_rev = date %m+% months(3),
         date_q_plus_3_rev = quarter(date %m+% months(3)))
#>        cusip       date date_q date_q_plus_3 date_plus_3_rev
#> 1   69021210 2008-07-31      3             4      2008-10-31
#> 2   50187A10 2006-12-29      4             1      2007-03-29
#> 3   77437410 2011-02-28      1             2      2011-05-28
#> 4   54928210 2013-03-28      1             2      2013-06-28
#> 5   23204G10 2017-02-28      1             2      2017-05-28
#> 6   78409V10 2015-12-31      4             1      2016-03-31
#> 7   68268N10 2016-02-29      1             2      2016-05-29
#> 8   22637210 2012-06-29      2             3      2012-09-29
#> 9   25400W10 2018-01-31      1            NA      2018-04-30
#> 10  02752P10 2015-09-30      3             4      2015-12-30
#> 11  82481R10 2018-11-30      4            NA      2019-02-28
#> 12  12811R10 2013-05-31      2             3      2013-08-31
#> 13  60871R20 2009-03-31      1            NA      2009-06-30
#> 14  84649R10 2008-12-31      4             1      2009-03-31
#> 15  00215F10 2008-02-29      1             2      2008-05-29
#> 16  52490110 2014-08-29      3             4      2014-11-29
#> 17  21868310 2015-01-30      1             2      2015-04-30
#> 18  44133910 2009-01-30      1             2      2009-04-30
#> 19  00971T10 2015-09-30      3             4      2015-12-30
#> 20  86667410 2015-08-31      3            NA      2015-11-30
#> 21  51070010 2010-12-31      4             1      2011-03-31
#> 22  57174810 2012-06-29      2             3      2012-09-29
#> 23  72383630 2017-11-30      4            NA      2018-02-28
#> 24  92220710 2010-08-31      3            NA      2010-11-30
#> 25  14446010 2006-11-30      4            NA      2007-02-28
#> 26  66611T10 2010-11-30      4            NA      2011-02-28
#> 27  52077610 2016-05-31      2             3      2016-08-31
#> 28  83547010 2009-05-29      2             3      2009-08-29
#> 29  45798520 2017-10-31      4             1      2018-01-31
#> 30  74346Y10 2008-07-31      3             4      2008-10-31
#> 31  40424G10 2006-05-31      2             3      2006-08-31
#> 32  78430510 2012-04-30      2             3      2012-07-30
#> 33  40425J10 2005-07-29      3             4      2005-10-29
#> 34  57734510 2013-04-30      2             3      2013-07-30
#> 35  89678F10 2016-03-31      1            NA      2016-06-30
#> 36  84863T10 2016-03-31      1            NA      2016-06-30
#> 37  92342Y10 2016-02-29      1             2      2016-05-29
#> 38  76218Y10 2015-10-30      4             1      2016-01-30
#> 39  21075N20 2009-04-30      2             3      2009-07-30
#> 40  78377T10 2018-02-28      1             2      2018-05-28
#> 41  87156510 2005-12-30      4             1      2006-03-30
#> 42  22100610 2012-08-31      3            NA      2012-11-30
#> 43  50076Q10 2012-11-30      4            NA      2013-02-28
#> 44  63633D10 2015-04-30      2             3      2015-07-30
#> 45  44840710 2016-08-31      3            NA      2016-11-30
#> 46  G7496G10 2009-10-30      4             1      2010-01-30
#> 47  31374720 2008-02-29      1             2      2008-05-29
#> 48  40412C10 2015-10-30      4             1      2016-01-30
#> 49  71375U10 2017-03-31      1            NA      2017-06-30
#> 50  87185110 2008-09-30      3             4      2008-12-30
#> 51  21075N20 2006-01-31      1            NA      2006-04-30
#> 52  67058H10 2017-05-31      2             3      2017-08-31
#> 53  78644920 2014-05-30      2             3      2014-08-30
#> 54  50187A10 2011-06-30      2             3      2011-09-30
#> 55  86608210 2018-12-31      4             1      2019-03-31
#> 56  87874D10 2006-05-31      2             3      2006-08-31
#> 57  00095710 2018-11-30      4            NA      2019-02-28
#> 58  65339F10 2010-08-31      3            NA      2010-11-30
#> 59  61539420 2008-01-31      1            NA      2008-04-30
#> 60  50207910 2016-04-29      2             3      2016-07-29
#> 61  77571110 2005-02-28      1             2      2005-05-28
#> 62  56280310 2015-04-30      2             3      2015-07-30
#> 63  47012E10 2005-06-30      2             3      2005-09-30
#> 64  01877R10 2015-03-31      1            NA      2015-06-30
#> 65  G6331P10 2012-02-29      1             2      2012-05-29
#> 66  37247D10 2018-06-29      2             3      2018-09-29
#> 67  40412C10 2015-06-30      2             3      2015-09-30
#> 68  44267D10 2012-10-31      4             1      2013-01-31
#> 69  45778Q10 2008-07-31      3             4      2008-10-31
#> 70  09180C10 2014-03-31      1            NA      2014-06-30
#> 71  03763V10 2015-05-29      2             3      2015-08-29
#> 72  75936P10 2005-10-31      4             1      2006-01-31
#> 73  03073H10 2010-08-31      3            NA      2010-11-30
#> 74  00476410 2008-09-30      3             4      2008-12-30
#> 75  67623R10 2008-10-31      4             1      2009-01-31
#> 76  57308310 2010-07-30      3             4      2010-10-30
#> 77  00770C10 2016-05-31      2             3      2016-08-31
#> 78  58353820 2007-02-28      1             2      2007-05-28
#> 79  58155Q10 2018-03-29      1             2      2018-06-29
#> 80  78129510 2010-01-29      1             2      2010-04-29
#> 81  30214U10 2016-03-31      1            NA      2016-06-30
#> 82  42234710 2006-04-28      2             3      2006-07-28
#> 83  48242W10 2014-05-30      2             3      2014-08-30
#> 84  63938C10 2014-07-31      3             4      2014-10-31
#> 85  27000610 2007-07-31      3             4      2007-10-31
#> 86  14067E50 2008-03-31      1            NA      2008-06-30
#> 87  83545R20 2007-09-28      3             4      2007-12-28
#> 88  91529Y10 2005-07-29      3             4      2005-10-29
#> 89  02752P10 2014-09-30      3             4      2014-12-30
#> 90  29273R10 2012-10-31      4             1      2013-01-31
#> 91  66680710 2012-01-31      1            NA      2012-04-30
#> 92  49458010 2006-08-31      3            NA      2006-11-30
#> 93  36269P10 2006-09-29      3             4      2006-12-29
#> 94  71376K10 2013-11-29      4            NA      2014-02-28
#> 95  05379B10 2010-12-31      4             1      2011-03-31
#> 96  67612W10 2006-03-31      1            NA      2006-06-30
#> 97  89531P10 2012-03-30      1             2      2012-06-30
#> 98  04916420 2010-09-30      3             4      2010-12-30
#> 99  58844810 2014-12-31      4             1      2015-03-31
#> 100 34484910 2018-11-30      4            NA      2019-02-28
#>     date_q_plus_3_rev
#> 1                   4
#> 2                   1
#> 3                   2
#> 4                   2
#> 5                   2
#> 6                   1
#> 7                   2
#> 8                   3
#> 9                   2
#> 10                  4
#> 11                  1
#> 12                  3
#> 13                  2
#> 14                  1
#> 15                  2
#> 16                  4
#> 17                  2
#> 18                  2
#> 19                  4
#> 20                  4
#> 21                  1
#> 22                  3
#> 23                  1
#> 24                  4
#> 25                  1
#> 26                  1
#> 27                  3
#> 28                  3
#> 29                  1
#> 30                  4
#> 31                  3
#> 32                  3
#> 33                  4
#> 34                  3
#> 35                  2
#> 36                  2
#> 37                  2
#> 38                  1
#> 39                  3
#> 40                  2
#> 41                  1
#> 42                  4
#> 43                  1
#> 44                  3
#> 45                  4
#> 46                  1
#> 47                  2
#> 48                  1
#> 49                  2
#> 50                  4
#> 51                  2
#> 52                  3
#> 53                  3
#> 54                  3
#> 55                  1
#> 56                  3
#> 57                  1
#> 58                  4
#> 59                  2
#> 60                  3
#> 61                  2
#> 62                  3
#> 63                  3
#> 64                  2
#> 65                  2
#> 66                  3
#> 67                  3
#> 68                  1
#> 69                  4
#> 70                  2
#> 71                  3
#> 72                  1
#> 73                  4
#> 74                  4
#> 75                  1
#> 76                  4
#> 77                  3
#> 78                  2
#> 79                  2
#> 80                  2
#> 81                  2
#> 82                  3
#> 83                  3
#> 84                  4
#> 85                  4
#> 86                  2
#> 87                  4
#> 88                  4
#> 89                  4
#> 90                  1
#> 91                  2
#> 92                  4
#> 93                  4
#> 94                  1
#> 95                  1
#> 96                  2
#> 97                  2
#> 98                  4
#> 99                  1
#> 100                 1

Created on 2019-09-27 by the reprex package (v0.2.1)

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.