lubridate

Floor a year to the decade in R

∥☆過路亽.° 提交于 2019-11-30 08:20:07
I would like to floor a set of dates to the nearest decade, e.g: 1922 --> 1920, 2099 --> 2090, etc. I was hoping I could do this in Lubridate, as in: floor_date(1922, 'decade') But I get: Error in match.arg(unit) : 'arg' should be one of “second”, “minute”, “hour”, “day”, “week”, “month”, “year” Is there any way to do this gracefully, perhaps avoiding a bunch of if-else statement to do the binning, and hopefully avoiding a bunch of cut s to do the grouping? Floor a Year in R to nearest decade: Think of Modulus as a way to extract the rightmost digit and use it to subtract from the original

Have lubridate subtraction return only a numeric value

社会主义新天地 提交于 2019-11-30 07:56:04
问题 I have one variable called Started which is the date on which human subjects enrolled in a study and another variable called dos1 which is the date upon which the subject last had surgery. I want to work out how many months since their last surgery to the day of enrollment. I tried: as.period(syrrupan$Started-syrrupan$dos1,units=c("month")) I expected this to give me something like: 14, 18, 1, 26 With each number being the number of months. Instead I get: 1 year, -4 months, -5 days and -1

with_tz with a vector of timezones

╄→尐↘猪︶ㄣ 提交于 2019-11-30 05:11:44
问题 I have a dataframe like so: library(dplyr) data <- data_frame( timestamp_utc = c('2015-11-18 03:55:04', '2015-11-18 03:55:08', '2015-11-18 03:55:10'), local_tz = c('America/New_York', 'America/Los_Angeles', 'America/Indiana/Indianapolis') ) I need to create a new variable that converts the UTC timestamp to the local time as defined in the local_tz column. However, both format and with_tz (from lubridate ) expect only one timezone, not a vector of timezones. I'm looking for something like this

function naming conflicts

非 Y 不嫁゛ 提交于 2019-11-30 04:56:31
问题 What are some good patterns for development with packages that define the same function? In my case, lubridate and data.table both define wday . 回答1: You can use :: , it helps to specify which package to use: lubridate::wday function (x, label = FALSE, abbr = TRUE) UseMethod("wday") <environment: namespace:lubridate> data.table::wday function (x) as.POSIXlt(x)$wday + 1L <environment: namespace:data.table> 回答2: Use the namespace mechanism for your packages. See the R Extensions manual. 来源:

Aggregation by time period in lubridate

不打扰是莪最后的温柔 提交于 2019-11-30 03:59:13
问题 This question asks about aggregation by time period in R, what pandas calls resampling. The most useful answer uses the XTS package to group by a given time period, applying some function such as sum() or mean(). One of the comments suggested there was something similar in lubridate, but didn't elaborate. Can someone provide an idiomatic example using lubridate? I've read through the lubridate vignette a couple times and can imagine some combination of lubridate and plyr, however I want to

R Time periods overlapping

感情迁移 提交于 2019-11-29 22:03:27
问题 With "lubridate" package in R, I can find out if two time periods overlapped. but Is there an efficient way to compute for how many days they overlapped. (for instance how many days a women smoked while pregnant. the pregnancy period and smoking period may overlap totally, partially or not at all) Here is an example with three women: preg_start<-as.Date(c("2011-01-01","2012-01-01","2013-01-01")) preg_end<-preg_start+270 # end after 9 months smoke_start<-as.Date(c("2011-02-01","2012-08-01",

Extract Fiscal Year with R Lubridate

房东的猫 提交于 2019-11-29 15:44:36
I'll create several dates. library(lubridate) x <- ymd(c("2012-03-26", "2012-05-04", "2012-09-23", "2012-12-31")) I can extract the year and quarter from these x values. quarter(x, with_year = TRUE, fiscal_start = 10) [1] 2012.2 2012.3 2012.4 2013.1 But I can't seem to extract just the fiscal year. This doesn't work, but what will? year(x, with_year = TRUE, fiscal_start = 10) I receive the following error message: Error in year(x, with_year = TRUE, fiscal_start = 10) : unused arguments (with_year = TRUE, fiscal_start = 10) If you don't mind an additional step, you could then extract the first

Format multiple date formats in one columns using lubridate

柔情痞子 提交于 2019-11-29 14:57:56
Sometimes I am given data sets that has two different date formats but common variables that have to been joined into one dataframe. Over the years, I've tried various solutions to get around this workflow hassle. Now that I've been using lubridate, it seems like many of these problems are easily solved. However, I am encountering some behaviour that seems weird to me though I imagine there is a good explanation that is beyond me. Say I am given a data set with different date formats that I join into one data frame. This dataframe looks like this: library(ludridate) library(dplyr) df<-data

Extract week number from POSIXct object

隐身守侯 提交于 2019-11-29 14:28:39
问题 Is there a function in lubridate to extract the week number? I've tried to search for that but couldn't find anything which serves the purpose. The week() function does something different. Description Date-time must be a POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, zoo, zooreg, timeDate, xts, > its, ti, jul, timeSeries, and fts objects. Weeks is the number of complete seven day periods that have occured between the date and January 1st, plus one. isoweek returns the week as it would

Floor a year to the decade in R

▼魔方 西西 提交于 2019-11-29 11:18:40
问题 I would like to floor a set of dates to the nearest decade, e.g: 1922 --> 1920, 2099 --> 2090, etc. I was hoping I could do this in Lubridate, as in: floor_date(1922, 'decade') But I get: Error in match.arg(unit) : 'arg' should be one of “second”, “minute”, “hour”, “day”, “week”, “month”, “year” Is there any way to do this gracefully, perhaps avoiding a bunch of if-else statement to do the binning, and hopefully avoiding a bunch of cut s to do the grouping? 回答1: Floor a Year in R to nearest