lubridate

Create a Vector of All Days Between Two Dates

…衆ロ難τιáo~ 提交于 2019-11-26 15:42:36
问题 Is there an easy way in R for me to itemize all valid days that occurred between two specified dates? For instance, I'd like the following inputs: itemizeDates(startDate="12-30-11", endDate="1-4-12") To produce the following dates: "12-30-11" "12-31-11", "1-1-12", "1-2-12", "1-3-12", "1-4-12" I'm flexible on classes and formatting of the dates, I just need an implementation of the concept. 回答1: You're looking for seq > seq(as.Date("2011-12-30"), as.Date("2012-01-04"), by="days") [1] "2011-12

How to determine if date is a weekend or not (not using lubridate)

孤者浪人 提交于 2019-11-26 11:35:45
问题 I have a vector of date objects ( yyyy-mm-dd ) and I want to determine if any of them are on weekend or not. Is there a function that can determine this straightaway? I can use wday() in the lubridate package and then determine if returned value is 01 or 07 , but anything else more straightforward? x <- seq(Sys.Date()-10, Sys.Date(), by = 1) x[lubridate::wday(x) %in% c(1, 7)] 回答1: I put @AnandaMahto's suggestion here rather than a comment: library(chron) x <- seq(Sys.Date()-10, Sys.Date(), by

Assigning Dates to Fiscal Year

旧时模样 提交于 2019-11-26 11:16:30
问题 I\'m trying to come up with some code that will look at a date and then assign it to a fiscal year. I\'m totally stuck. I have a variable that contains dates in POSIXct format: df$Date #2015-05-01 CST #2015-04-30 CST #2014-09-01 CST What I need to be able to do is take those dates and return a fiscal year, which runs from May 1 - April 30. For example, Fiscal Year 2016 runs 2015-05-01 through 2016-04-30. Results would look something like this: df$Date df$FiscalYear #2015-05-01 CST #FY2016

Transform year/week to date object

一曲冷凌霜 提交于 2019-11-26 08:53:40
String contains 'YEAR WEEK' and I want to transform it with parse_date_time() to a date object but I can't make the code work: parse_date_time(c("201510"), "YW") I don't have to use lubridate , can be other packages, too. Before converting year-week to a date you have to specify a day of the week but more importantly you have to ensure which of the different conventions is being used. Base R's strptime() function knows 3 definitions for week of the year (but supports only 2 of them on input) and 2 definitions for weekday numbers, see ?strptime : Week of the year US convention : Week of the

Convert dd/mm/yy and dd/mm/yyyy to Dates

匆匆过客 提交于 2019-11-26 08:35:07
问题 I have some a character vector with dates in various formats like this dates <- c(\"23/11/12\", \"20/10/2012\", \"22/10/2012\" ,\"23/11/12\") I want to convert these to Dates. I have tried the very good dmy from the lubridate package, but this does not work: dmy(dates) [1] \"0012-11-23 UTC\" \"2012-10-20 UTC\" \"2012-10-22 UTC\" \"0012-11-23 UTC\" It is treating the /12 year as if it is 0012. So I now am trying regular expression to select each type and individually convert to dates using as

How to flatten / merge overlapping time periods

耗尽温柔 提交于 2019-11-26 05:54:12
问题 I have a large data set of time periods, defined by a \'start\' and and an \'end\' column. Some of the periods overlap. I would like to combine (flatten / merge / collapse) all overlapping time periods to have one \'start\' value and one \'end\' value. Some example data: ID start end 1 A 2013-01-01 2013-01-05 2 A 2013-01-01 2013-01-05 3 A 2013-01-02 2013-01-03 4 A 2013-01-04 2013-01-06 5 A 2013-01-07 2013-01-09 6 A 2013-01-08 2013-01-11 7 A 2013-01-12 2013-01-15 Desired result: ID start end 1

How to convert Excel date format to proper date with Lubridate

流过昼夜 提交于 2019-11-26 05:30:32
问题 I\'m working with a csv which unfortunately has logged datetimes using the number format of 42705 although it should be 01/12/2016. I\'d like to convert it to the right format in R using Lubridate or some other package. Is there a function that will handle it? 回答1: You don't need to use lubridate for this, the base function as.Date handles this type of conversion nicely. The trick is that you have to provide the origin, which in Excel is December 30, 1899. as.Date(42705, origin = "1899-12-30"

Convert character to Date in R

拈花ヽ惹草 提交于 2019-11-26 03:58:31
问题 I am relatively new to R, but this is the first time I have had to deal with date conversions. I read in my data from a CSV (using read.table()), but I shorted the data to highlight my issue. When read into R, the Date field is character. Simply, most of my dates get coerced correctly, except for a few instances. The example below will hopefully show you what is going on. # my attempt to coerce the date -- uses the stringr package prods.all$Date2 <- as.Date(str_sub(prods.all$Date, 1, str

Transform year/week to date object

强颜欢笑 提交于 2019-11-26 01:09:03
问题 String contains \'YEAR WEEK\' and I want to transform it with parse_date_time() to a date object but I can\'t make the code work: parse_date_time(c(\"201510\"), \"YW\") I don\'t have to use lubridate , can be other packages, too. 回答1: Before converting year-week to a date you have to specify a day of the week but more importantly you have to ensure which of the different conventions is being used. Base R's strptime() function knows 3 definitions for week of the year (but supports only 2 of