Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

r - Remove weekend data in a dataframe

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...