lubridate

convert to date and strip time?

◇◆丶佛笑我妖孽 提交于 2019-12-05 13:06:39
I have some data that looks like this: dates <- structure(c(1L, 2L, 4L, 3L), .Label = c("Sat, 18 Nov 2017 00:00:00 GMT", "Thu, 16 Nov 2017 00:00:00 GMT", "Tue, 14 Nov 2017 00:00:00 GMT", "Wed, 15 Nov 2017 00:00:00 GMT"), class = "factor") I would like to convert it to a date format instead of having it as a factor. Additionally, I want to strip the 00:00:00 GMT because it is meaningless I tried lubridate but I'm having troubles with the format: library(lubridate) mdy(dates) Warning message: All formats failed to parse. No formats found. This looks like it is working: as.POSIXct(dates, format =

changing POSIXct date vaules to first day of each week

穿精又带淫゛_ 提交于 2019-12-05 07:13:05
I want to calculate the average Dist for each week using these data (below) while preserving the benefits of a using the POSIXct time class. df <- structure(list(IndID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), class = "factor", .Label = "AAA"), Date = structure(c(1329436800, 1329458400, 1329480000, 1329501600, 1329523200, 1329544800, 1329566400, 1329588000, 1329609600, 1329631200, 1329652800, 1329674400, 1329696000,

Why R package lubridate can't parse vector with multiple formats?

百般思念 提交于 2019-12-05 05:52:29
I'm using package lubridate to parse a vector of heterogeneously-formatted dates and convert them to string, like this: parse_date_time(c('12/17/1996 04:00:00 PM','4/18/1950 0130'), c('%m/%d/%Y %I:%M:%S %p','%m/%d/%Y %H%M')) This is the result: [1] NA NA Warning message: All formats failed to parse. No formats found. If I remove the %p in the 1st format string, it incorrectly parses the 1st date string, and still doesn't parse the 2nd, like so: [1] "1996-12-17 04:00:00 UTC" NA Warning message: 1 failed to parse. The 4PM time in the string is parsed to 4AM in the result. Has anyone experienced

Why are my functions on lubridate dates so slow?

孤人 提交于 2019-12-05 05:11:40
I wrote this function which I use all the time: # Give the previous day, or Friday if the previous day is Saturday or Sunday. previous_business_date_if_weekend = function(my_date) { if (length(my_date) == 1) { if (weekdays(my_date) == "Sunday") { my_date = lubridate::as_date(my_date) - 2 } if (weekdays(my_date) == "Saturday") { my_date = lubridate::as_date(my_date) - 1 } return(lubridate::as_date(my_date)) } else if (length(my_date) > 1) { my_date = lubridate::as_date(sapply(my_date, previous_business_date_if_weekend)) return(my_date) } } Problems arise when I apply it to a date column of a

How to get week starting date from a date in R [duplicate]

不问归期 提交于 2019-12-05 03:42:42
问题 This question already has answers here : How to find Previous Sunday in R (4 answers) Closed 2 years ago . I have a dataset with a column containing dates. I want to find the week starting dates for those date values. I get the week number using week function from lubridate . For example, week(as.Date("04/20/2017", "%m/%d/%Y")) #Solution [1] 16 Instead of weeknum , is there a way to get the starting date of the week? In this case I am expecting either "04/16/2017" or "04/17/2017". I am not

Determine season from Date using lubridate in R

独自空忆成欢 提交于 2019-12-05 01:58:24
问题 I have a very big dataset with a DateTime Column containing POSIXct-Values. I need to determine the season (Winter - Summer) based on the DateTime column. I've created a function which works fine on a small dataset, but crashes when I use it on the large one. Can anybody see my mistake? I've created 4 functions: 3 subfunctions so that I can do logical comparisons and selection using *apply functions 1 function to determine the season Here are thefunctions: require(lubridate) # function for

Length of lubridate interval

馋奶兔 提交于 2019-12-04 20:53:26
问题 What's the best way to get the length of time represented by an interval in lubridate , in specified units? All I can figure out is something like the following messy thing: > ival [1] 2011-01-01 03:00:46 -- 2011-10-21 18:33:44 > difftime(attr(ival, "start") + as.numeric(ival), attr(ival, "start"), 'days') Time difference of 293.6479 days (I also added this as a feature request at https://github.com/hadley/lubridate/issues/105, under the assumption that there's no better way available - but

Format Date to Year-Month in R

你说的曾经没有我的故事 提交于 2019-12-04 14:03:27
I would like to retain my current date column in year-month format as date. It currently gets converted to chr format. I have tried as_datetime but it coerces all values to NA. The format I am looking for is: "2017-01" library(lubridate) df<- data.frame(Date=c("2017-01-01","2017-01-02","2017-01-03","2017-01-04", "2018-01-01","2018-01-02","2018-02-01","2018-03-02"), N=c(24,10,13,12,10,10,33,45)) df$Date <- as_datetime(df$Date) df$Date <- ymd(df$Date) df$Date <- strftime(df$Date,format="%Y-%m") Thanks in advance! lubridate only handle dates, and dates have days. However, as alistaire mentions,

Matching values between data frames based on overlapping dates

廉价感情. 提交于 2019-12-04 05:29:00
问题 I am currently dealing with the following data structures: Attributes df: ID Begin_A End_A Interval Value 1 5 1990-03-01 2017-03-10 1990-03-01 UTC--2017-03-10 UTC Cat1 2 10 1993-12-01 2017-12-02 1993-12-01 UTC--2017-12-02 UTC Cat2 3 5 1991-03-01 2017-03-03 1991-03-01 UTC--2017-03-03 UTC Cat3 4 10 1995-12-05 2017-12-10 1995-12-05 UTC--2017-12-10 UTC Cat4 Bookings df: ID Begin_A End_A Interval 1 5 2017-03-03 2017-03-05 2017-03-03 UTC--2017-03-05 UTC 2 6 2017-05-03 2017-05-05 2017-05-03 UTC-

R / lubridate: Calculate number of overlapping days between two periods

依然范特西╮ 提交于 2019-12-04 02:52:56
问题 I am trying to calculate the number of overlapping days between two time periods. One period is fixed in a start and end date, the other is recorded as start and end dates in a data frame. Edit: I'm dealing with ads that have publish date (df$start) and an unpublish date (df$end). What I'm trying to find out is how many days they have been online in a specific month (my.start = 2018-01-01, my.end = 2018-08-31). library(dplyr) library(lubridate) my.start <- ymd("2018-08-01") my.end <- ymd(