首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从R中的日期序列中选择一个月中最早的日期?

如何从R中的日期序列中选择一个月中最早的日期?
EN

Stack Overflow用户
提问于 2019-10-14 12:01:52
回答 2查看 431关注 0票数 0

我有一个数据库,其中包含不同频率(每周,每月,每天)数据的不同指数的值。我希望通过从时间序列中提取月初的值来计算月度回报。

我尝试使用循环逐月对时间序列进行分区,然后使用min()获取该月中的最早日期。然而,我想知道是否有更有效的方法来加速计算。

代码语言:javascript
复制
library(data.table)
df<-fread("statistic_date index_value funds_number
           2013-1-1    1000.000            0
           2013-1-4     996.096           21
           2013-1-11    1011.141           21
           2013-1-18    1057.344           21
           2013-1-25    1073.376           21
           2013-2-1    1150.479           22
           2013-2-8    1150.288           19
           2013-2-22    1112.993           18
           2013-3-1    1148.826           20
           2013-3-8    1093.515           18
           2013-3-15    1092.352           17
           2013-3-22    1138.346           18
           2013-3-29    1107.440           17
           2013-4-3    1101.897           17
           2013-4-12    1093.344           17")

我希望筛选得到每个月的最早日期的行,例如:

代码语言:javascript
复制
2013-1-1    1000.000            0
2013-2-1    1150.479           22
2013-3-1    1148.826           20
2013-4-3    1101.897           17

您的帮助将不胜感激!

EN

回答 2

Stack Overflow用户

发布于 2019-10-14 12:09:08

使用tidyverse和lubridate包,

代码语言:javascript
复制
library(lubridate)
library(tidyverse)
df %>% mutate(statistic_date = ymd(statistic_date), # convert statistic_date to date format
              month = month(statistic_date),  #create month and year columns
              year= year(statistic_date)) %>%
       group_by(month,year) %>% # group by month and year
       arrange(statistic_date) %>% # make sure the df is sorted by date
       filter(row_number()==1) # select first row within each group



# A tibble: 4 x 5
# Groups:   month, year [4]
#  statistic_date index_value funds_number month  year
#  <date>               <dbl>        <int> <dbl> <dbl>
#1 2013-01-01           1000             0     1  2013
#2 2013-02-01           1150.           22     2  2013
#3 2013-03-01           1149.           20     3  2013
#4 2013-04-03           1102.           17     4  2013
票数 0
EN

Stack Overflow用户

发布于 2019-10-14 17:23:28

首先,让statistic_date成为一个日期:

代码语言:javascript
复制
df$statistic_date <- as.Date(df$statistic_date)

您可以在statistic_date中使用nth_day查找每个月的第一天。

代码语言:javascript
复制
library("datetimeutils")
dates <- nth_day(df$statistic_date, period = "month", n = "first")
## [1] "2013-01-01" "2013-02-01" "2013-03-01" "2013-04-03"

df[statistic_date %in% dates]
##    statistic_date index_value funds_number
## 1:     2013-01-01    1000.000            0
## 2:     2013-02-01    1150.479           22
## 3:     2013-03-01    1148.826           20
## 4:     2013-04-03    1101.897           17
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58369953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档