问题描述
我正在尝试使用dplyr
和lubridate
过滤当月的当前日期和当月的第一天。由于某些奇怪的原因,我似乎无法正确过滤该月的第一天。我只能提取每月的当前日期(Date == Sys.Date
)。谁能指出我的正确方向,而不是我在这里做错了什么?
data %>% filter(Date == floor_date(Sys.Date(),'month') | Date == Sys.Date())
这是我的数据集(data
)
structure(list(Date = structure(c(18122,18123,18124,18127,18128,18129,18130,18131,18134,18135,18136,18137,18138,18141,18142,18143,18144,18145,18148,18149,18150,18151,18152,18155,18156,18157,18158,18159,18162,18163,18164,18165,18166,18169,18170,18171,18172,18173,18176,18177,18178,18179,18180,18183,18184,18185,18186,18187,18190,18191,18192,18193,18194,18197,18198,18199,18200,18201,18204,18205,18206,18207,18208,18211,18212,18213,18214,18215,18218,18219,18220,18221,18222,18225,18226,18227,18228,18229,18232,18233,18234,18235,18236,18239,18240,18241,18242,18243,18246,18247,18248,18249,18250,18253,18254,18255,18256,18257,18260,18261,18262,18263,18264,18267,18268,18269,18270,18271,18274,18275,18276,18277,18278,18281,18282,18283,18284,18285,18288,18289,18290,18291,18292,18295,18296,18297,18298,18299,18302,18303,18304,18305,18306,18309,18310,18311,18312,18313,18316,18317,18318,18319,18320,18323,18324,18325,18326,18327,18330,18331,18332,18333,18334,18337,18338,18339,18340,18341,18344,18345,18346,18347,18348,18351,18352,18353,18354,18355,18358,18359,18360,18361,18362,18365,18366,18367,18368,18369,18372,18373,18374,18375,18376,18379,18380,18381,18382,18383,18386,18387,18388,18389,18390,18393,18394,18395,18396,18397,18400,18401,18402,18403,18404,18407,18408,18409,18410,18411,18414,18415,18416,18417,18418,18421,18422,18423,18424,18425,18428,18429,18430,18431,18432,18435,18436,18437,18438,18439,18442,18443,18444,18445,18446,18449,18450,18451,18452,18453,18456,18457,18458,18459,18460,18463,18464,18465,18466,18467,18470,18471,18472,18473,18474,18477,18478,18479,18480,18481,18484,18485,18486,18487,18488),class = "Date"),`EUR-ZAR` = c(17.1786,16.9625,16.9623,17.1039,17.0507,16.8403,16.8875,16.9746,16.9281,16.9701,17.0584,16.9336,16.6856,16.719,16.5601,16.3306,16.4193,16.3137,16.3132,16.1692,16.1859,16.1261,16.26,16.1303,16.2788,16.1964,16.3505,16.4145,16.3684,16.3783,16.4091,16.4073,16.5648,16.4901,16.7468,16.7416,16.621,16.5033,16.659,16.7383,16.7151,16.5305,16.3349,16.3382,16.4232,16.5415,16.4949,16.5309,16.4403,16.2323,16.2857,16.3008,16.2053,16.1522,16.2455,16.7019,16.8317,16.7727,16.4713,16.342,16.3889,16.2536,16.3677,16.4115,16.4231,16.4036,16.3341,16.2459,16.3792,16.3316,16.3741,16.2386,16.2109,16.2691,16.3048,16.2427,16.1978,16.1722,16.1067,16.2107,16.1759,16.2496,16.1719,16.2235,16.4079,16.3401,16.1575,16.1278,15.9924,16.0404,15.8794,15.8068,15.7569,15.7524,15.7031,15.7015,15.6823,15.683,15.7949,15.6952,15.6954,15.737,15.9717,15.9149,15.927,15.7244,15.7906,15.9698,16.0174,16.0041,16.0309,16.0453,16.0693,16.059,15.8889,15.9046,15.9346,16.0861,16.0127,16.0859,16.2748,16.6269,16.4558,16.3067,16.2238,16.3745,16.4821,16.3202,16.153,16.1417,16.2009,16.1346,16.2243,16.1816,16.2811,16.266,16.435,16.5702,16.6372,17.0262,17.2752,17.1897,17.2145,16.9919,17.4742,17.6835,18.3893,18.0364,18.2503,18.4685,18.0732,18.5828,18.2228,18.8339,18.5551,18.827,19.1586,18.9093,18.8365,19.0941,19.6372,19.7686,19.7002,19.9831,20.0233,20.529,20.1498,19.8547,19.7523,19.6939,19.6433,19.7405,20.0944,20.3403,20.3123,20.3716,20.4052,20.5847,20.5481,20.5899,20.5908,20.3126,20.1226,19.7388,20.2724,20.6856,20.1678,19.9391,20.2879,20.1205,19.8757,19.8851,19.9789,19.9776,19.9183,20.0801,20.0039,20.0225,19.6641,19.2638,19.2479,19.1915,19.0982,19.1326,19.3731,19.4937,19.3221,19.1704,18.9994,19.1692,19.0316,18.8152,18.8407,18.7749,19.4258,19.179,19.3685,19.3955,19.3,19.5502,19.3765,19.4864,19.4518,19.568,19.2321,19.3298,19.4072,19.4781,19.156,19.0789,19.1227,19.1907,19.3441,19.1941,18.9958,18.9444,19.0857,19.0556,18.9132,19.0699,19.0625,19.019,18.9119,19.0619,19.3363,19.4328,19.3019,19.3404,19.4958,19.8343,20.0239,20.2172,20.486,20.5646,20.6937,20.7893,20.7693,20.5407,20.5476,20.5583)),row.names = c(NA,-263L),class = c("tbl_df","tbl","data.frame"))
解决方法
您可以尝试以下解决方案。
假设您的数据框名称为df
df$DAY <- day(df$Date)
df1 <- subset(df,df$DAY=='1' | df$DAY == Sys.Date())
,
您可以尝试:
library(dplyr)
library(lubridate)
todays_date <- Sys.Date()
data %>%
filter(month(Date) == month(todays_date),year(Date) == year(todays_date)) %>%
arrange(Date) %>%
slice(1L,n())
这是第一个选择当前月份的数据,它基于arrange
Date
,并从中选择第一个和最后一个条目。