lubridate

Add/subtract 6 months (bond time) in R using lubridate

让人想犯罪 __ 提交于 2019-12-17 10:42:48
问题 I am looking to add and subtract six months reliably with lubridate . For example, adding six months to 12/31/2014 should result in 6/30/2015 , and adding to 2/28/2014 should result in 8/31/2014 The issue with as.Date("2014-12-31") + months(6) , is that it yields an NA . Alternatively, the second result is 8/28/2014 because it doesn't just add 6 months to the month and then know where the day should end up dependent upon the month. Is there any way to quickly correct this? At the moment, I am

extract hours and seconds from POSIXct for plotting purposes in R

﹥>﹥吖頭↗ 提交于 2019-12-17 10:21:10
问题 Suppose I have the following data.frame foo start.time duration 1 2012-02-06 15:47:00 1 2 2012-02-06 15:02:00 2 3 2012-02-22 10:08:00 3 4 2012-02-22 09:32:00 4 5 2012-03-21 13:47:00 5 And class(foo$start.time) returns [1] "POSIXct" "POSIXt" I'd like to create a plot of foo$duration v. foo$start.time . In my scenario, I'm only interested in the time of day rather than the actual day of the year. How does one go about extracting the time of day as hours:seconds from POSIXct class of vector? 回答1

Is there a more elegant way to convert two-digit years to four-digit years with lubridate?

三世轮回 提交于 2019-12-17 09:45:45
问题 If a date vector has two-digit years, mdy() turns years between 00 and 68 into 21st Century years and years between 69 and 99 into 20th Century years. For example: library(lubridate) mdy(c("1/2/54","1/2/68","1/2/69","1/2/99","1/2/04")) gives the following output: Multiple format matches with 5 successes: %m/%d/%y, %m/%d/%Y. Using date format %m/%d/%y. [1] "2054-01-02 UTC" "2068-01-02 UTC" "1969-01-02 UTC" "1999-01-02 UTC" "2004-01-02 UTC" I can fix this after the fact by subtracting 100 from

Generate a sequence of the last day of the month over two years

大兔子大兔子 提交于 2019-12-17 02:22:53
问题 I use lubridate and figured that this would be so easy ymd("2010-01-31")+months(0:23) But look what one gets. It is all messed up! [1] "2010-01-31 UTC" "2010-03-03 UTC" "2010-03-31 UTC" "2010-05-01 UTC" "2010-05-31 UTC" "2010-07-01 UTC" "2010-07-31 UTC" "2010-08-31 UTC" "2010-10-01 UTC" [10] "2010-10-31 UTC" "2010-12-01 UTC" "2010-12-31 UTC" "2011-01-31 UTC" "2011-03-03 UTC" "2011-03-31 UTC" "2011-05-01 UTC" "2011-05-31 UTC" "2011-07-01 UTC" [19] "2011-07-31 UTC" "2011-08-31 UTC" "2011-10-01

creating inteval object in r using lubridate package [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-14 03:32:29
问题 This question already has an answer here : indicateing to which interval a date belongs (1 answer) Closed 11 months ago . hi i have data from uber : about pick ups in NYC . im trying to add a column to the raw data, that indicates for each row, for which time interval (which is represented by a single timepoint at the beginning of thetime interval) it belongs. i want to Create a vector containing all relevant timepoints (i.e. every 15 minutes Use int_diff function from lubridate package on

Lubridate mdy function

夙愿已清 提交于 2019-12-14 02:23:07
问题 I'm trying to convert the following and am not successful with one of the dates [1]. "4/2/10" becomes "0010-04-02". Is there a way to correct this? thanks, Vivek data <- data.frame(initialDiagnose = c("4/2/10","14.01.2009", "9/22/2005", "4/21/2010", "28.01.2010", "09.01.2009", "3/28/2005", "04.01.2005", "04.01.2005", "Created on 9/17/2010", "03 01 2010")) mdy <- mdy(data$initialDiagnose) dmy <- dmy(data$initialDiagnose) mdy[is.na(mdy)] <- dmy[is.na(mdy)] # some dates are ambiguous, here we

lubridate `period` versus `as.numeric`

六眼飞鱼酱① 提交于 2019-12-13 17:58:14
问题 Is there any reason why I should prefer /dseconds() over as.numeric ? It seems the latter is a little bit faster. They both give the same results. > as.numeric(lubridate::ymd_hms("2015-12-31 23:59:59 UTC") - lubridate::ymd_hms("2015-01-01 00:00:00 UTC"), units = "secs") [1] 31535999 > interval(lubridate::ymd_hms("2015-01-01 00:00:00 UTC"), lubridate::ymd_hms("2015-12-31 23:59:59 UTC"))/dseconds(1) [1] 31535999 and the microbenchmark test: summary(microbenchmark::microbenchmark( as.numeric

R - week function returns unexpected values

谁说我不能喝 提交于 2019-12-13 07:24:19
问题 I am looking to find the week within a year in R for a list of dates. However, The outputs are rather weird. See below: week("2000-01-01") week("2000-01-06") week("2000-01-07") week("2000-01-13") week("2000-01-14") This returns 1, 1, 2, 2, 3 However, if you look at a calendar: http://calendars.generalblue.com/content/images/calendars/2000/01-January-2000-Calendar.png You'd expect it to be 1, 2, 2, 3, 3. For some reason, this rolls over on a Thursday. Isn't that strange? Does anyone know how I

Outputting various subsets from one data frame based on dates

萝らか妹 提交于 2019-12-13 06:41:13
问题 I want to create numerous subsets of data based on date sequences defined from a separate dataframe. For example, one dataframe will have dates and daily recorded values across multiple years. I have created a hypothetical dataframe below. I want to conduct various subsets from this dataframe based on start and end dates defined elsewhere. set.seed(24) df1 <- as.data.frame(matrix(sample(0:3000, 300*10, replace=TRUE), ncol=1)) df2 <- as.data.frame(seq(as.Date("2004/1/1"), by = "day", length

Flag rows with interval overlap in r

牧云@^-^@ 提交于 2019-12-13 03:57:42
问题 I have a df frame containing TV viewing data, I would like to run a QC check for overlapping viewing. Let's say for the same day, same household, for each individual, each minute should be credited to one station or channel only. for example, I would like to flag line 8 , 9 , because it seem impossible an individual in a unique household watched two TV stations (62,67) at the same time (start_hour_minute) . I am wondering is there a way to flag this rows? A sort of min by min view by