我有一個這樣的列日期資料集:
| 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 |
我想選擇日期小于 2 月 15 日的資料
我嘗試使用 ifelse 陳述句在我的資料庫中定義每年的 2 月 15 日,但我想知道是否有更簡單的方法!
這是我期待的結果
| ID | 日期 |
|---|---|
| 1 | 2018-02-13 |
| 3 | 2014-01-23 |
| 5 | 2022-01-23 |
提前致謝
uj5u.com熱心網友回復:
你可以使用lubridate
library(lubridate)
library(dplyr)
d %>% filter(month(date)<2 | (month(date)==2 & day(date)<=15))
輸出:
id date
1 1 2018-02-13
2 3 2014-01-23
3 5 2022-01-23
uj5u.com熱心網友回復:
這是一個基本的 R 選項。使用format檢查月份/年份是否在 1 月 1 日和 2 月 15 日之間。
df[between(format(df$date, "%m%d"), "0101", "0215"),]
輸出
id date
1 1 2018-02-13
3 3 2014-01-23
5 5 2022-01-23
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/456816.html
上一篇:R-將字串轉換為日期
