lubridate

How to create date from datetime (using lubridate)?

天涯浪子 提交于 2019-12-30 18:58:10
问题 Assume I have created a variable containing date and time: a <- ymd_hms("2014-01-01 12:23:34") How do I create another variable that only has the date? That is, what should I do to transform a 's value to be identical to b 's value where b is b <- ymd("2014-01-01") 回答1: You can just use round_date round_date(a, "day") [1] "2014-01-02 UTC" EDIT You do need to be careful with rounding the time though. For complete equivalence here you would need to use floor_date . identical(ymd("2014-01-01"),

How to create date from datetime (using lubridate)?

折月煮酒 提交于 2019-12-30 18:57:25
问题 Assume I have created a variable containing date and time: a <- ymd_hms("2014-01-01 12:23:34") How do I create another variable that only has the date? That is, what should I do to transform a 's value to be identical to b 's value where b is b <- ymd("2014-01-01") 回答1: You can just use round_date round_date(a, "day") [1] "2014-01-02 UTC" EDIT You do need to be careful with rounding the time though. For complete equivalence here you would need to use floor_date . identical(ymd("2014-01-01"),

Count the number of Fridays or Mondays in Month in R

被刻印的时光 ゝ 提交于 2019-12-30 08:15:04
问题 I would like a function that counts the number of specific days per month.. i.e.. Nov '13 -> 5 fridays.. while Dec'13 would return 4 Fridays.. Is there an elegant function that would return this? library(lubridate) num_days <- function(date){ x <- as.Date(date) start = floor_date(x, "month") count = days_in_month(x) d = wday(start) sol = ifelse(d > 4, 5, 4) #estimate that is the first day of the month is after Thu or Fri then the week will have 5 Fridays sol } num_days("2013-08-01") num_days

Valid time zones in lubridate

时光毁灭记忆、已成空白 提交于 2019-12-30 07:55:08
问题 A quick google search seems to get me nowhere. What are valid time zones in lubridate's tz option? In particular, am looking for Brasilia's time zone. Thanks! library(lubridate) dts <- c("6-3-1995 12:01:01","29-3-1995 23:01:01","29-3-1995 20:01:01") dmy_hms(dts) # locale's tz default dmy_hms(dts, tz = "chile") # Chilean time (has one time zone only) 回答1: Take a search through OlsonNames() which provides a list of all the valid timezones on the host system. e.g.: grep("Brazil",OlsonNames()

Valid time zones in lubridate

那年仲夏 提交于 2019-12-30 07:55:07
问题 A quick google search seems to get me nowhere. What are valid time zones in lubridate's tz option? In particular, am looking for Brasilia's time zone. Thanks! library(lubridate) dts <- c("6-3-1995 12:01:01","29-3-1995 23:01:01","29-3-1995 20:01:01") dmy_hms(dts) # locale's tz default dmy_hms(dts, tz = "chile") # Chilean time (has one time zone only) 回答1: Take a search through OlsonNames() which provides a list of all the valid timezones on the host system. e.g.: grep("Brazil",OlsonNames()

R - Filter 1st Dataframe with conditions of timestamp from DF2

北城余情 提交于 2019-12-25 18:25:01
问题 DF1: X Y DateTime 1 113.8591 22.25272 2016-01-07 10:37:33 2 113.8585 22.25276 2016-01-07 10:37:43 3 113.8578 22.25270 2016-01-07 10:37:53 4 113.8572 22.25265 2016-02-01 11:34:03 5 113.8565 22.25260 2016-02-18 12:20:13 6 113.8559 22.25251 2016-02-18 12:20:23 structure(list(Date = c("2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016-10-27", "2016

Find dates within a period interval by group

我只是一个虾纸丫 提交于 2019-12-25 09:29:46
问题 I have a panel with many IDs, begin and end dates. begin to end date create an interval of time. id begin end interval overlap 1: 1 2010-01-31 2011-06-30 2009-08-04 UTC--2011-12-27 UTC TRUE 2: 1 2011-01-31 2012-06-30 2010-08-04 UTC--2012-12-27 UTC TRUE 3: 1 2012-01-31 2013-06-30 2011-08-04 UTC--2013-12-27 UTC TRUE 4: 1 2013-01-31 2014-06-30 2012-08-04 UTC--2014-12-27 UTC TRUE 5: 1 2013-02-28 2013-07-31 2012-09-01 UTC--2014-01-27 UTC TRUE 6: 1 2015-02-28 2015-03-31 2014-09-01 UTC--2015-09-27

lubridate - messages

删除回忆录丶 提交于 2019-12-24 23:26:37
问题 Will it be possible to suppress messages such as "Using date format..." when using a function like? > ymd(vec) Using date format %Y%m%d Whilst these are good to see when you are casting a vector, it can be annoying in some circumstances. 回答1: Looking at the ymd code, it callse parse_date , which gives those annoying messages via the command message . Looking at ?message , there is a corresponding suppressMessages : suppressMessages(ymd(x)) (Note - other similar functions are suppressWarnings

Change multiple character columns to date

蹲街弑〆低调 提交于 2019-12-24 18:33:24
问题 I have multiple character columns (around 20) that I would like to change all to date formats and drop the time using r. I've tried loops , mutate and apply . Here is some sample data using just two columns col1 = c("2017-04-01 23:00:00", "2017-03-03 00:00:01", "2017-04-02 00:00:01") col2 = c("2017-04-10 08:41:49", "2017-04-10 08:39:48", "2017-04-10 08:41:51") df <- cbind(col1, col2) I've tried: df <- df %>% mutate(df, funs(ymd)) and df <- df %>% mutate(df, funs(mdy)) Both gave me an error. I

Why are my csv file dates not parsing via mdy (lubridate)

纵然是瞬间 提交于 2019-12-24 16:10:06
问题 I'm hoping someone can shed some light on why lubridate isn't parsing my dates correctly. I'm reading in a fairly large csv file to a data frame, so my issue isn't necessarily reproducible, but I'll show my steps: require("lubridate") pricingAddy = "C/DailyData.csv" pricingData = as.data.frame(read.csv(pricingAddy, header = TRUE, stringsAsFactors = FALSE)) sampleHead = head(pricingData) sampleHead Symbol TradeDate PX_OPEN PX_HIGH PX_LOW PX_LAST PX_VOLUME MOV_AVG_20D MOV_AVG_50D MOV_AVG_100D