![](/img/trans.png)
[英]How to specify day and month for messy date data with missing day and month when converting to date in large data frame
[英]Select data based on a day in a month, "If date is lesser than a day in a month"
我有一個包含日期列的數據集,如下所示:
ID | 日期 |
---|---|
1個 | 2018-02-13 |
2個 | 2019-02-28 |
3個 | 2014-01-23 |
4個 | 2021-10-28 |
5個 | 2022-01-23 |
6個 | 2019-09-28 |
我想要 select 日期小於 2 月 15 日的數據
我嘗試使用 ifelse 語句在我的數據庫中定義每年的 2 月 15 日,但我想知道是否有更簡單的方法!
這是我期待的結果
ID | 日期 |
---|---|
1個 | 2018-02-13 |
3個 | 2014-01-23 |
5個 | 2022-01-23 |
提前致謝
你可以使用lubridate
library(lubridate)
library(dplyr)
d %>% filter(month(date)<2 | (month(date)==2 & day(date)<=15))
Output:
id date
1 1 2018-02-13
2 3 2014-01-23
3 5 2022-01-23
這是一個基本的 R 選項。 使用format
檢查月份/年份是否在 1 月 1 日和 2 月 15 日之間。
df[between(format(df$date, "%m%d"), "0101", "0215"),]
Output
id date
1 1 2018-02-13
3 3 2014-01-23
5 5 2022-01-23
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.