lubridate

Find day of year with the lubridate package in R

烂漫一生 提交于 2019-12-04 02:47:09
问题 I'm looking to find the day of year for a POSIXct class object with lubridate. For example, 12-9-2015 is day 343. It's easy to find the day of the week or month with lubridate: > lubridate::wday("2015-12-09 04:27:56 EST", labels = T) Wed > lubridate::day("2015-12-09 04:27:56 EST") 9 Is there an easy way to do so for the day of the year? I've searched the documentation and other questions but have not (yet) found an answer. 回答1: The correct function is yday , as in lubridate::yday(Sys.time())

Using the result of summarise (dplyr) to mutate the original dataframe

隐身守侯 提交于 2019-12-03 18:05:13
问题 I have a rather big dataframe with a column of POSIXct datetimes (~10yr of hourly data). I would flag all the rows in which the day falls in a Daylight saving period. For example if the Daylight shift starts on '2000-04-02 03:00:00' (DOY=93) i would like that the two previous hours of DOY=93 could be flagged. Although I am a newbie of dplyr I would use this package as much as possible and avoid for-loops as much as possible For example: library(lubridate) sd = ymd('2000-01-01',tz="America

Determine season from Date using lubridate in R

百般思念 提交于 2019-12-03 16:19:02
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 logical comparison (to be used in *apply) greaterOrEqual <- function(x,y){ ifelse(x >= y,T,F) } #

Length of lubridate interval

空扰寡人 提交于 2019-12-03 14:55:46
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 maybe someone here knows of one.) Update - apparently the difftime function doesn't handle this either.

In R, use lubridate to convert hms objects into seconds

北战南征 提交于 2019-12-03 13:26:44
simple question in lubridate--I want to convert an hms object into its appropriate number of seconds since the start of the day. For instance library(lubridate) hms("12:34:45") then I want to know exactly how long 12 hours, 34 minutes, and 45 seconds is, in seconds something obvious like seconds(hms("12:34:45")) just returns 45s which is not what I want. How do I convert these hms values into seconds? I'd like to use lubridate It doesn't matter which package you use -- it will have convert a date / datetime object into a POSIXct representation of seconds since the epoch. So you may as well do

R lubridate converting seconds to date

家住魔仙堡 提交于 2019-12-03 10:04:36
I have a simple question regarding R's lubridate package. I've a series of timestamps in seconds since epoch. I want to convert this to YYYY-MM-DD-HH format. In base R, I can do something like this to first convert it to a date format > x = as.POSIXct(1356129107,origin = "1970-01-01",tz = "GMT") > x [1] "2012-12-21 22:31:47 GMT" Note the above just converts it to a date format, not the YYYY-MM-DD-HH format. How would I do this in lubridate? How would I do it using base R? Thanks much in advance Dirk is correct. However, if you are intent on using lubridate functions: paste( year(dt), month(dt)

Extract Month from Week and Year number [duplicate]

只谈情不闲聊 提交于 2019-12-02 18:52:02
问题 This question already has answers here : Transform year/week to date object (2 answers) Get the month from the week of the year (3 answers) Closed last year . I have a dataframe where I extracted the week and year number using lubridate . I do not have the date values anymore. I want to extract the month from the week number and year number. df Week Year 1 2018 5 2018 45 2017 I want my final output to be this: Week Year Month 1 2018 1 5 2018 2 45 2017 11 回答1: This extracts the month of the

Dealing with numeric (decimal) dates in R?

安稳与你 提交于 2019-12-02 16:04:28
问题 I have some numeric date data from Excel: > df <- data.frame(c(42613, 42612, 42614), c(42614.61, 42613.97, 42612.12)) > names(df) <- c("Closetime", "Opentime") Both Closetime and Opentime are numeric . I want to preserve the hour/minute/second data from OpenTime and add the time '00:00:00' to every date in Closetime : > df$Closetime <- paste(as.Date(df$Closetime, origin = '1900-01-01'), c('00:00:00')) Easy enough to do Closetime , but when I try using lubridate::date_decimal on Opentime ,

Converting numeric to date issues

我怕爱的太早我们不能终老 提交于 2019-12-02 13:03:30
I have a date column that is numeric as follows Date1 4010 5178 5494 6750 7106 39 39 172 1704 4152 I am trying to convert this to real date using this function as.Date(df$Date1, origin = "1970-01-01") However I am seeing that these numbers are converted to these dates which are incorrect. Date1_Converted 1980-12-24 1984-03-26 1985-01-16 1988-06-25 1989-06-16 1970-02-09 1970-02-09 1970-06-22 1974-09-01 1981-05-15 The correct transformation should have been Date1_CrctTrnsf 2005.10.31 2009.02.11 2009.12.04 2013.05.15 2014.05.06 1994.02.22 1994.02.22 1994.08.03 1999.05.03 2006.03.22 I am assuming

Recognize time in R

一世执手 提交于 2019-12-02 12:03:45
问题 I am working on a dataset in R with a time variable like this: Time = data.frame("X1"=c(930,1130,914,1615)) The first one/two digits of X1 refers to hour and the last two refers to minute. I want to make R recognize it as a time variable. I try to use lubridate hm function but it didnt work probably because a ":" is missing between the hour and minute in my data. I also thought about using str_sub function to separate the hour and minute first and then put them together with a ":" in between