删除数据框中的周末数据

发布于 2024-11-07 04:50:00 字数 2014 浏览 0 评论 0原文

正如您从下面的数据框中看到的,RBloomberg 返回周末日期的 NA。

如果是周末,我想删除整行。我该怎么做?

我不想使用 na.omit 因为如果/当我出于合法原因在数据中获得 NA 时,这可能会删除工作日行。

   ticker       date yld_ytm_mid
1    R206 2011-05-11       6.946
2    R206 2011-05-12       6.969
3    R206 2011-05-13       7.071
4    R206 2011-05-14          NA
5    R206 2011-05-15          NA
6    R201 2011-05-11       7.201
7    R201 2011-05-12       7.213
8    R201 2011-05-13       7.323
9    R201 2011-05-14          NA
10   R201 2011-05-15          NA
11   R157 2011-05-11       7.611
12   R157 2011-05-12       7.622
13   R157 2011-05-13       7.718
14   R157 2011-05-14          NA
15   R157 2011-05-15          NA
16   R203 2011-05-11       8.165
17   R203 2011-05-12       8.170
18   R203 2011-05-13       8.279
19   R203 2011-05-14          NA
20   R203 2011-05-15          NA
21   R204 2011-05-11       8.303
22   R204 2011-05-12       8.296
23   R204 2011-05-13       8.386
24   R204 2011-05-14          NA
25   R204 2011-05-15          NA
26   R207 2011-05-11       8.361
27   R207 2011-05-12       8.371
28   R207 2011-05-13       8.479
29   R207 2011-05-14          NA
30   R207 2011-05-15          NA
31   R208 2011-05-11       8.392
32   R208 2011-05-12       8.393
33   R208 2011-05-13       8.514
34   R208 2011-05-14          NA
35   R208 2011-05-15          NA
36   R186 2011-05-11       8.546
37   R186 2011-05-12       8.571
38   R186 2011-05-13       8.664
39   R186 2011-05-14          NA
40   R186 2011-05-15          NA
41   R213 2011-05-11       8.783
42   R213 2011-05-12       8.802
43   R213 2011-05-13       8.898
44   R213 2011-05-14          NA
45   R213 2011-05-15          NA
46   R209 2011-05-11       8.785
47   R209 2011-05-12       8.807
48   R209 2011-05-13       8.898
49   R209 2011-05-14          NA
50   R209 2011-05-15          NA
51   R214 2011-05-11       8.841
52   R214 2011-05-12       8.861
53   R214 2011-05-13       8.958
54   R214 2011-05-14          NA
55   R214 2011-05-15          NA

As you can see from the dataframe below, RBloomberg returns NAs for weekend dates.

I want to remove the entire row if it falls on a weekend. How would I do this?

I don't want to use the na.omit as this might remove weekday rows if/when I get an NA there in the data for a legitimate reason.

   ticker       date yld_ytm_mid
1    R206 2011-05-11       6.946
2    R206 2011-05-12       6.969
3    R206 2011-05-13       7.071
4    R206 2011-05-14          NA
5    R206 2011-05-15          NA
6    R201 2011-05-11       7.201
7    R201 2011-05-12       7.213
8    R201 2011-05-13       7.323
9    R201 2011-05-14          NA
10   R201 2011-05-15          NA
11   R157 2011-05-11       7.611
12   R157 2011-05-12       7.622
13   R157 2011-05-13       7.718
14   R157 2011-05-14          NA
15   R157 2011-05-15          NA
16   R203 2011-05-11       8.165
17   R203 2011-05-12       8.170
18   R203 2011-05-13       8.279
19   R203 2011-05-14          NA
20   R203 2011-05-15          NA
21   R204 2011-05-11       8.303
22   R204 2011-05-12       8.296
23   R204 2011-05-13       8.386
24   R204 2011-05-14          NA
25   R204 2011-05-15          NA
26   R207 2011-05-11       8.361
27   R207 2011-05-12       8.371
28   R207 2011-05-13       8.479
29   R207 2011-05-14          NA
30   R207 2011-05-15          NA
31   R208 2011-05-11       8.392
32   R208 2011-05-12       8.393
33   R208 2011-05-13       8.514
34   R208 2011-05-14          NA
35   R208 2011-05-15          NA
36   R186 2011-05-11       8.546
37   R186 2011-05-12       8.571
38   R186 2011-05-13       8.664
39   R186 2011-05-14          NA
40   R186 2011-05-15          NA
41   R213 2011-05-11       8.783
42   R213 2011-05-12       8.802
43   R213 2011-05-13       8.898
44   R213 2011-05-14          NA
45   R213 2011-05-15          NA
46   R209 2011-05-11       8.785
47   R209 2011-05-12       8.807
48   R209 2011-05-13       8.898
49   R209 2011-05-14          NA
50   R209 2011-05-15          NA
51   R214 2011-05-11       8.841
52   R214 2011-05-12       8.861
53   R214 2011-05-13       8.958
54   R214 2011-05-14          NA
55   R214 2011-05-15          NA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄与心 2024-11-14 04:50:00

为了完整起见,我想在blindjesse的答案中添加一点,输入?weekdays表明R有基本函数weekdays()、months()和quarters(),它们适用于posix和date类型,并且我相信是矢量化的,所以这也会起作用:

!(weekdays(as.Date(date)) %in% c('Saturday','Sunday'))

For completeness' sake, I would add to blindjesse's answer that typing ?weekdays reveals that R has base functions weekdays(), months() and quarters() that work on both the posix and date types, and are I believe vectorized, so this would work as well:

!(weekdays(as.Date(date)) %in% c('Saturday','Sunday'))
玩世 2024-11-14 04:50:00

将日期列转换为 POSIXlt ,例如

date <- as.POSIXlt(date,format="%Y-%m-%d")

来访问星期几

date$wday

然后您可以使用适当的子集

Convert the date column to a POSIXlt ,eg

date <- as.POSIXlt(date,format="%Y-%m-%d")

Then you can access the day of the week using

date$wday

and subset the frame appropriately

陌上芳菲 2024-11-14 04:50:00

blindJesse 的答案是正确且有用的,因为它回归到基本 R 函数。

许多包都有额外的辅助包装器。这是来自 timeDate 的一个,需要转换为其类型:

R> isWeekend( as.timeDate( seq( as.Date("2011-01-01"), 
+                               to=as.Date("2011-01-07"), by=1 ) ) )
2011-01-01 2011-01-02 2011-01-03 2011-01-04 2011-01-05 2011-01-06 2011-01-07  
      TRUE       TRUE      FALSE      FALSE      FALSE      FALSE      FALSE  
R> 

这是另一种使用来自 RcppBDT 的函数:

R> sapply(seq(as.Date("2011-01-01"),to=as.Date("2011-01-07"), by=1),getDayOfWeek)
[1] 6 0 1 2 3 4 5
R> 
R> sapply(seq(as.Date("2011-01-01"),to=as.Date("2011-01-07"), by=1),getDayOfWeek)
+         %%6 == 0
[1]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
R> 

lubridate 包也有 wday() 并且毫无疑问在其他包中还有更多的功能。

The answer by blindJesse is correct and useful as it falls back to base R functions.

Many packages have additional helper wrappers. Here is one from timeDate which requires conversion to its type:

R> isWeekend( as.timeDate( seq( as.Date("2011-01-01"), 
+                               to=as.Date("2011-01-07"), by=1 ) ) )
2011-01-01 2011-01-02 2011-01-03 2011-01-04 2011-01-05 2011-01-06 2011-01-07  
      TRUE       TRUE      FALSE      FALSE      FALSE      FALSE      FALSE  
R> 

and here is another approach using a function from RcppBDT:

R> sapply(seq(as.Date("2011-01-01"),to=as.Date("2011-01-07"), by=1),getDayOfWeek)
[1] 6 0 1 2 3 4 5
R> 
R> sapply(seq(as.Date("2011-01-01"),to=as.Date("2011-01-07"), by=1),getDayOfWeek)
+         %%6 == 0
[1]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
R> 

The lubridate package also has wday() and there are undoubtedly more he;per functions in other packages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文