lubridate

convert date to year month representation in R

孤者浪人 提交于 2019-12-20 01:05:09
问题 I have a Date , and am interested in representing it as an integer of yyyymm form. Currently, I do: get_year_month <- function(d) { return(as.integer(format(d, "%Y%m")))} mydate = seq.Date(from=as.Date("2012-01-01"), to=as.Date("5012-01-01"), by=1) system.time(ym <- get_year_month(mydate)) # user system elapsed # 5.972 0.974 6.951 This is very slow for large datasets. Is there a faster way? Please provide timings for your answers so they can be easily compared. Use the above example. 回答1:

Parse dates in format dmy together with dmY using parse_date_time

非 Y 不嫁゛ 提交于 2019-12-19 17:25:50
问题 I have a vector of character representation of dates, where formats mostly are dmY (e.g. 27-09-2013), dmy (e.g. 27-09-13), and occasionally some b or B months. Thus, parse_date_time in package lubridate that "allows the user to specify several format-orders to handle heterogeneous date-time character representations" could be a very useful function for me. However, it seems that parse_date_time has problem parsing dmy dates when they occur together with dmY dates. When parsing dmy alone, or

R aggregate a dataframe by hours from a date with time field

喜你入骨 提交于 2019-12-18 17:48:32
问题 I'm relatively new to R but I am very familiar with Excel and T-SQL. I have a simple dataset that has a date with time and a numeric value associated it. What I'd like to do is summarize the numeric values by-hour of the day. I've found a couple resources for working with time-types in R but I was hoping to find a solution similar to is offered excel (where I can call a function and pass-in my date/time data and have it return the hour of the day). Any suggestions would be appreciated -

How to get a date from day of year

不羁的心 提交于 2019-12-18 16:56:47
问题 I am working with a data set that looks a bit like this: Year Date Day_nr Value 1976 19-02-1976 50 167 1976 19-03-1976 79 140 1978 05-03-1978 64 200 1978 05-04-1978 95 200 1999 05-05-1999 125 89 1999 20-06-1999 171 79 I am then interested to estimate a polynomial model for each year depending on the day number as a x value. I then run a predict function to estimate the values of the model. I do that with the day numbers. My Data for the predict data frame then looks a bit like this just with

Changing lubridate function to start on Monday rather than Sunday

佐手、 提交于 2019-12-18 13:31:34
问题 dates <- NULL date <- as.Date("01/01/2014","%d/%m/%Y") dates <- data.frame(date=as.Date(character()) ,cal_day_in_year_num = numeric() ,cal_week_id = numeric() ,cal_week_start_date = as.Date(character()) ,cal_week_end_date = as.Date(character()) ) for (i in 1:365) { dates[i,1] <- date + days(i-1) ## date dates[i,2] <- yday(dates[i,1]) ## cal_day_in_year_num dates[i,3] <- paste(year(dates[i,1]),sprintf("%02d",week(dates[i,1])),sep="") ## cal_week_id dates[i,4] <- floor_date(dates[i,1], "week")

How to extract Month from date in R

旧时模样 提交于 2019-12-17 23:27:07
问题 I am using the lubridate package and applying the month function to extract month from date. I ran the str command on date field and I got Factor w/ 9498 levels "01/01/1979","01/01/1980",..: 5305 1 1 1 1 1 1 1 1 1 ... > v1$Date<-month(v1$Date) Error in as.POSIXlt.character(as.character(x), ...) : character string is not in a standard unambiguous format Here is an example of my data frame https://drive.google.com/file/d/0B6cqWmwsEk20Q2dHblhXZi14Wk0/edit?usp=sharing I don't know what I am doing

R sequence of dates with lubridate

我怕爱的太早我们不能终老 提交于 2019-12-17 23:26:06
问题 Hi I'm trying to get a sequence of dates with lubridate This doesn't work seq(ymd('2012-04-07'),ymd('2013-03-22'),by=week(1)) the base command seq(as.Date('2012-04-7'),as.Date('2013-03-22'),'weeks') does, but I'd like to know if there is an elegant way to do this with lubridate. EDIT Please ignore : solved myself so leaving up for posterity only. Happy to have this deleted if necessary. seq(ymd('2012-04-07'),ymd('2013-03-22'),by='weeks') Does the trick 回答1: ymd is a wrapper to parse date

Efficient and accurate age calculation (in years, months, or weeks) in R given birth date and an arbitrary date

杀马特。学长 韩版系。学妹 提交于 2019-12-17 16:14:17
问题 I am facing the common task of calculating the age (in years, months, or weeks) given the date of birth and an arbitrary date. The thing is that quite often I have to do this over many many records (>300 millions), so performance is a key issue here. After a quick search in SO and Google I found 3 alternatives: A common arithmetic procedure (/365.25) (link) Using functions new_interval() and duration() from package lubridate (link) Function age_calc() from package eeptools (link, link, link)

dplyr, lubridate : how to aggregate a dataframe by week?

你说的曾经没有我的故事 提交于 2019-12-17 10:43:09
问题 Consider the following example library(tidyverse) library(lubridate) time <- seq(from =ymd("2014-02-24"),to= ymd("2014-03-20"), by="days") set.seed(123) values <- sample(seq(from = 20, to = 50, by = 5), size = length(time), replace = TRUE) df2 <- data_frame(time, values) df2 <- df2 %>% mutate(day_of_week = wday(time, label = TRUE)) Source: local data frame [25 x 3] time values day_of_week <date> <dbl> <fctr> 1 2014-02-24 30 Mon 2 2014-02-25 45 Tues 3 2014-02-26 30 Wed 4 2014-02-27 50 Thurs 5

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

只谈情不闲聊 提交于 2019-12-17 10:42:49
问题 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