如何在 R 中从日期中找到一年中的第几天?
要从日期中找到一年中的第几天,我们可以使用 lubridate 包的 yday 函数。例如,如果我们有日期或向量的日期,那么我们只需使用下面的语法将该日期或向量传递给 yday 函数即可 -
yday(“date”)
或者
yday(“vector_of_date”)
加载 lubridate 包 -
library(lubridate)
示例
date1<-"2020-01-01" yday(date1) [1] 1 date2<-"2020-01-25" yday(date2) [1] 25 date3<-c("2020-01-25","2020-01-15","2020-02-25","2020-10-03","2020-02-25","2020-05-15","2020-01-28","2020-01-20","2020-11-25","2020-12-25","2020-01-05","2020-06-21","2020-06-22","2020-02-20","2020-03-18","2020-04-19","2020-04-04","2020-04-29","2020-01-03","2020-01-17","2020-11-05","2020-09-09","2020-10-09","2020-04-14","2020-10-02","2020-12-03","2020-11-07","2020-08-15","2020-07-22","2020-07-25","2020-08-25","2020-05-07","2020-05-23","2020-04-01","2020-04-30","2020-07-15","2020-08-17","2020-09-12","2020-10-03","2020-11-16","2020-01-11","2020-06-06","2020-07-24") date3 [1] "2020-01-25" "2020-01-15" "2020-02-25" "2020-10-03" "2020-02-25" [6] "2020-05-15" "2020-01-28" "2020-01-20" "2020-11-25" "2020-12-25" [11] "2020-01-05" "2020-06-21" "2020-06-22" "2020-02-20" "2020-03-18" [16] "2020-04-19" "2020-04-04" "2020-04-29" "2020-01-03" "2020-01-17" [21] "2020-11-05" "2020-09-09" "2020-10-09" "2020-04-14" "2020-10-02" [26] "2020-12-03" "2020-11-07" "2020-08-15" "2020-07-22" "2020-07-25" [31] "2020-08-25" "2020-05-07" "2020-05-23" "2020-04-01" "2020-04-30" [36] "2020-07-15" "2020-08-17" "2020-09-12" "2020-10-03" "2020-11-16" [41] "2020-01-11" "2020-06-06" "2020-07-24" yday(date3) [1] 25 15 56 277 56 136 28 20 330 360 5 173 174 51 78 110 95 120 3 [20] 17 310 253 283 105 276 338 312 228 204 207 238 128 144 92 121 197 230 256 [39] 277 321 11 158 206 date4<-seq(as.Date("2020-01-01"),as.Date("2020-04-01"),by="days") date4 [1] "2020-01-01" "2020-01-02" "2020-01-03" "2020-01-04" "2020-01-05" [6] "2020-01-06" "2020-01-07" "2020-01-08" "2020-01-09" "2020-01-10" [11] "2020-01-11" "2020-01-12" "2020-01-13" "2020-01-14" "2020-01-15" [16] "2020-01-16" "2020-01-17" "2020-01-18" "2020-01-19" "2020-01-20" [21] "2020-01-21" "2020-01-22" "2020-01-23" "2020-01-24" "2020-01-25" [26] "2020-01-26" "2020-01-27" "2020-01-28" "2020-01-29" "2020-01-30" [31] "2020-01-31" "2020-02-01" "2020-02-02" "2020-02-03" "2020-02-04" [36] "2020-02-05" "2020-02-06" "2020-02-07" "2020-02-08" "2020-02-09" [41] "2020-02-10" "2020-02-11" "2020-02-12" "2020-02-13" "2020-02-14" [46] "2020-02-15" "2020-02-16" "2020-02-17" "2020-02-18" "2020-02-19" [51] "2020-02-20" "2020-02-21" "2020-02-22" "2020-02-23" "2020-02-24" [56] "2020-02-25" "2020-02-26" "2020-02-27" "2020-02-28" "2020-02-29" [61] "2020-03-01" "2020-03-02" "2020-03-03" "2020-03-04" "2020-03-05" [66] "2020-03-06" "2020-03-07" "2020-03-08" "2020-03-09" "2020-03-10" [71] "2020-03-11" "2020-03-12" "2020-03-13" "2020-03-14" "2020-03-15" [76] "2020-03-16" "2020-03-17" "2020-03-18" "2020-03-19" "2020-03-20" [81] "2020-03-21" "2020-03-22" "2020-03-23" "2020-03-24" "2020-03-25" [86] "2020-03-26" "2020-03-27" "2020-03-28" "2020-03-29" "2020-03-30" [91] "2020-03-31" "2020-04-01" yday(date4) [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 [51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 [76] 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 date5<-seq(as.Date("2020-01-01"),as.Date("2020-06-01"),by="2 days") date5 [1] "2020-01-01" "2020-01-03" "2020-01-05" "2020-01-07" "2020-01-09" [6] "2020-01-11" "2020-01-13" "2020-01-15" "2020-01-17" "2020-01-19" [11] "2020-01-21" "2020-01-23" "2020-01-25" "2020-01-27" "2020-01-29" [16] "2020-01-31" "2020-02-02" "2020-02-04" "2020-02-06" "2020-02-08" [21] "2020-02-10" "2020-02-12" "2020-02-14" "2020-02-16" "2020-02-18" [26] "2020-02-20" "2020-02-22" "2020-02-24" "2020-02-26" "2020-02-28" [31] "2020-03-01" "2020-03-03" "2020-03-05" "2020-03-07" "2020-03-09" [36] "2020-03-11" "2020-03-13" "2020-03-15" "2020-03-17" "2020-03-19" [41] "2020-03-21" "2020-03-23" "2020-03-25" "2020-03-27" "2020-03-29" [46] "2020-03-31" "2020-04-02" "2020-04-04" "2020-04-06" "2020-04-08" [51] "2020-04-10" "2020-04-12" "2020-04-14" "2020-04-16" "2020-04-18" [56] "2020-04-20" "2020-04-22" "2020-04-24" "2020-04-26" "2020-04-28" [61] "2020-04-30" "2020-05-02" "2020-05-04" "2020-05-06" "2020-05-08" [66] "2020-05-10" "2020-05-12" "2020-05-14" "2020-05-16" "2020-05-18" [71] "2020-05-20" "2020-05-22" "2020-05-24" "2020-05-26" "2020-05-28" [76] "2020-05-30" "2020-06-01" yday(date5) [1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 [20] 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 [39] 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 [58] 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 [77] 153
广告