在R中给定日期范围之间找到每个月的开始和结束日期 [英] Finding start and end date of each month between given date range in R

查看:90
本文介绍了在R中给定日期范围之间找到每个月的开始和结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R编程的初学者,希望获得有关对数据集执行日期运算的帮助(希望可以在R中执行此操作),



我已将给定的数据集导入为交易

 交易<-read.csv( deals_dates.csv)

有数据(输入),

  Deal_Id Deal_Name开始日期结束日期
1 Samsung Mobile 3/5/2018 6/23/2018
2 Apple Watch 2017年12月21日2/9/2018

我想查找每个交易在给定起始日期/结束日期范围之间的每月开始和结束日期,



(预期输出)

  Deal_Id Deal_Name起始日期结束日期
1 Samsung Mobile 3 / 5/2018 3/31/2018
1三星移动4/1/2018 4/30/2018
1三星移动5/1/2018 5/31/2018
1三星移动6 / 1/2018 6/23/2018
2 Apple Watch 2017年12月21日2017年12月31日
2苹果手表2018年1月1日1/31/2018
2苹果手表2/1/2018 2018年9月9日

谢谢!

解决方案

您也可以仅使用 Vanilla R 的东西。例如,使用以下函数:

  myDateFunction< -function(dealName,stdate,endate){

firstDay<-切(as.Date(stdate,format =%m /%d /%Y), month)
Start< -seq(as.Date(firstDay),as .Date(endate,format =%m /%d /%Y),by = month)
DealName<-rep(dealName,length(Start))
plusOne< -seq (as.Date(firstDay),by = month,长度= 2)[2]
End <-seq(as.Date(plusOne),length = 4,by = months)-1

data<-data.frame(DealName,Start,End)
data $ Start [1]<-as.Date(stdate,format =%m /%d /% Y)
data $ End [-1] [3]<-as.Date(endate,format =%m /%d /%Y)
return(data)

}




如果我们这样称呼方式:




  myDateFunction( Samsung Mobile, 3/5 / 2018, 6/23/2018)




产生以下输出:




  DealName开始结束
1 Samsung Mobile 2018-03-05 2018-03-31
2 Samsung Mobile 2018- 04-01 2018-04-30
3三星移动2018-05-01 2018-05-31
4三星移动2018-06-01 2018-06-23


I am a beginner to R programming and want help to perform date operation on a dataset (hoping that there could be a way to do it in R),

I have imported the given dataset as 'deals'

deals <- read.csv("deals_dates.csv")

with data (input),

Deal_Id  Deal_Name       Start_Date   End_Date
1        Samsung Mobile  3/5/2018     6/23/2018
2        Apple Watch     12/21/2017   2/9/2018

I want to find the monthly start and end date between given start_date/end_date range for each deal,

(expected output)

Deal_Id  Deal_Name       Start_Date   End_Date
1        Samsung Mobile  3/5/2018     3/31/2018
1        Samsung Mobile  4/1/2018     4/30/2018
1        Samsung Mobile  5/1/2018     5/31/2018
1        Samsung Mobile  6/1/2018     6/23/2018
2        Apple Watch     12/21/2017   12/31/2017
2        Apple Watch     1/1/2018     1/31/2018
2        Apple Watch     2/1/2018     2/9/2018

Thank you!

解决方案

You can also do something using just Vanilla R. For example with the following function:

myDateFunction <-function(dealName,stdate,endate){

 firstDay <- cut(as.Date(stdate,format = "%m/%d/%Y"), "month")
 Start <-seq(as.Date(firstDay), as.Date(endate,format = "%m/%d/%Y"), by = "month")
 DealName <- rep(dealName,length(Start))
 plusOne<-seq(as.Date(firstDay), by = "month", length = 2)[2]
 End<-seq(as.Date(plusOne),length=4,by="months")-1

 data <- data.frame(DealName,Start,End)
 data$Start[1] <- as.Date(stdate,format = "%m/%d/%Y")
 data$End[-1][3] <- as.Date(endate,format = "%m/%d/%Y")
 return(data)

}

If we call it this way:

myDateFunction("Samsung Mobile","3/5/2018","6/23/2018")

Produces the following output:

    DealName      Start        End
1 Samsung Mobile 2018-03-05 2018-03-31
2 Samsung Mobile 2018-04-01 2018-04-30
3 Samsung Mobile 2018-05-01 2018-05-31
4 Samsung Mobile 2018-06-01 2018-06-23

这篇关于在R中给定日期范围之间找到每个月的开始和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆