Skip to content

Instantly share code, notes, and snippets.

@yuu-ito
Created October 7, 2013 07:52
Show Gist options
  • Save yuu-ito/6864013 to your computer and use it in GitHub Desktop.
Save yuu-ito/6864013 to your computer and use it in GitHub Desktop.
日付型の操作メモ
seq(as.Date("2013-01-01"), len=12, by="1 month")
# [1] "2013-01-01" "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01" "2013-06-01" "2013-07-01" "2013-08-01"
# [9] "2013-09-01" "2013-10-01" "2013-11-01" "2013-12-01"
seq(as.Date("2013-01-01"), len=12, by="-1 month")
# [1] "2013-01-01" "2012-12-01" "2012-11-01" "2012-10-01" "2012-09-01" "2012-08-01" "2012-07-01" "2012-06-01"
# [9] "2012-05-01" "2012-04-01" "2012-03-01" "2012-02-01"
# 文字列日付 --> 1ヶ月前の日付に
tail(seq(as.Date("2013-09-30"), len=2, by="-1 month"),1)
next_month<-function(d,reverse=F,num=1){
# d<-as.Date('2013-02-01')
# next_month(d)
d<-as.Date(paste(substr(d,1,7),"01",sep="-"))
if(reverse==T){
return(tail(seq(d, len=2, by="-1 month"),1))
}
return(tail(seq(d, len=2, by="1 month"),1))
}
end_of_month <- function(d){
# d<-as.Date('2013-02-01')
# end_of_the_month(d)
return(tail(seq(next_month(d), len=2, by="-1 day"),1))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment