posixct

Convert Date to POSIXct

送分小仙女□ 提交于 2019-11-29 10:03:20
Why does the Date below change to "2014-07-07" when converted to POSIXct? Sys.setenv(TZ='America/Sao_Paulo') d <- as.Date("2014-07-08", format="%Y-%m-%d") d [1] "2014-07-08" as.POSIXct(d) [1] "2014-07-07 21:00:00 BRT" Because as.POSIXct.Date doesn't look for a timezone (and won't pass it to .POSIXct if you specify it in ... ) and Date objects are "UTC", so your POSIXct is offset from the UTC of the Date object. It would be better to call as.POSIXct on the character string directly, if you can: > as.POSIXct("2014-07-08", format="%Y-%m-%d") [1] "2014-07-08 BRT" 来源: https://stackoverflow.com

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

房东的猫 提交于 2019-11-29 08:08:41
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/Denver") ed = ymd('2005-12-31',tz="America/Denver") span = data.frame(date=seq(from=sd,to=ed, by="hour"))

How to add/subtract time from a POSIXlt time while keeping its class in R?

久未见 提交于 2019-11-29 06:20:47
问题 I am manipulating some POSIXlt DateTime objects. For example I would like to add an hour: my.lt = as.POSIXlt("2010-01-09 22:00:00") new.lt = my.lt + 3600 new.lt # [1] "2010-01-09 23:00:00 EST" class(new.lt) # [1] "POSIXct" "POSIXt" The thing is I want new.lt to be a POSIXlt object. I know I could use as.POSIXlt to convert it back to POSIXlt , but is there a more elegant and efficient way to achieve this? 回答1: Short answer: No Long answer: POSIXct and POSIXlt objects are two specific types of

Is there a fast parser for date

一世执手 提交于 2019-11-29 04:48:36
For datetimes fasttime provides very fast parsing to POSIXct library('fasttime') library('lubridate') library('microbenchmark') # parse character to POSIXct Sys.setenv(TZ='UTC') test <- rep('2011-04-02 11:01:00',1e4) microbenchmark( test1 <- fastPOSIXct(test), test2 <- fast_strptime(test,format='%Y-%m-%d %H:%M:%S'), test3 <- as.POSIXct(test, format='%Y-%m-%d %H:%M:%S'), test4 <- ymd_hms(test), times=100) Unit: microseconds expr min lq mean median uq max test1 <- fastPOSIXct(test) 663.123 692.337 1409.448 701.821 712.4965 71231.585 test2 <- fast_strptime(test, format = "%Y-%m-%d %H:%M:%S") 1026

Exclude specific time periods in R

ぐ巨炮叔叔 提交于 2019-11-29 04:43:20
I'm a beginner with R and have tried searching for data extraction for certain time periods but can't seem to find anything. I have a time series of continuous data measured at 10 minute intervals for a period of five months. For simplicity's sake, the data is available in two columns as follows: Timestamp Temp.Diff 2/14/2011 19:00 -0.385 2/14/2011 19:10 -0.535 2/14/2011 19:20 -0.484 2/14/2011 19:30 -0.409 2/14/2011 19:40 -0.385 2/14/2011 19:50 -0.215 ... And it goes on for the next five months. I have read the Timestamp column using as.POSIXct() into R. Assuming that only certain times of the

Milliseconds in POSIXct Class

微笑、不失礼 提交于 2019-11-29 01:37:01
How can I parse milliseconds correctly? as.POSIXct function works as following in my environment. > as.POSIXct("2014-02-24 11:30:00.001") [1] "2014-02-24 11:30:00.000 JST" > as.POSIXct("2014-02-24 11:30:00.0011") [1] "2014-02-24 11:30:00.001 JST" My R version is x86 v3.0.2 for Windows. Specify the input format, using %OS to represent the seconds with their fractional parts. x <- c("2014-02-24 11:30:00.123", "2014-02-24 11:30:00.456") y <- as.POSIXct(x, format = "%Y-%m-%d %H:%M:%OS") When you come to display the value, append a number between 0 and 6 to the format string to tell R how many

understanding dates/times (POSIXc and POSIXct) in R

点点圈 提交于 2019-11-29 00:14:21
I'm reading a table and it contains strings that describe timestamps. I just want to convert from string to a built-in datetime type... R> Q <- read.table(textConnection(' tsstring 1 "2009-09-30 10:00:00" 2 "2009-09-30 10:15:00" 3 "2009-09-30 10:35:00" 4 "2009-09-30 10:45:00" 5 "2009-09-30 11:00:00" '), as.is=TRUE, header=TRUE) R> ts <- strptime(Q$tsstring, "%Y-%m-%d %H:%M:%S", tz="UTC") if I try to store the datetime column into the data.frame, I get a curious error: R> Q$ts <- ts Error in `$<-.data.frame`(`*tmp*`, "ts", value = list(sec = c(0, 0, 0, : replacement has 9 rows, data has 5 but

Adding time to POSIXct object in R

自作多情 提交于 2019-11-28 19:06:13
I would like to add 1 hour to a POSIXct object, but it does not support '+'. This command: as.POSIXct("2012/06/30","GMT") + as.POSIXct(paste(event_hour, event_minute,0,":"), ,"%H:%M:$S") returns this error: Error in `+.POSIXt`(as.POSIXct("2012/06/30", "GMT"), as.POSIXct(paste(event_hour, : binary '+' is not defined for "POSIXt" objects How can I add a few hours to a POSIXct object ? James POSIXct objects are a measure of seconds from an origin, usually the UNIX epoch (1st Jan 1970). Just add the requisite number of seconds to the object: x <- Sys.time() x [1] "2012-08-12 13:33:13 BST" x + 3*60

Problems adding a month to X using POSIXlt in R - need to reset value using as.Date(X)

早过忘川 提交于 2019-11-28 13:09:06
问题 This works for me in R: # Setting up the first inner while-loop controller, the start of the next water year NextH2OYear <- as.POSIXlt(firstDate) NextH2OYear$year <- NextH2OYear$year + 1 NextH2OYear<-as.Date(NextH2OYear) But this doesn't: # Setting up the first inner while-loop controller, the start of the next water month NextH2OMonth <- as.POSIXlt(firstDate) NextH2OMonth$mon <- NextH2OMonth$mon + 1 NextH2OMonth <- as.Date(NextH2OMonth) I get this error: Error in as.Date.POSIXlt(NextH2OMonth

Get the month from the week of the year

大城市里の小女人 提交于 2019-11-28 09:36:12
问题 Let's say we have this: ex <- c('2012-41') This represent the week 41 from the year 2012. How would I get the month from this? Since a week can be between two months, I will be interested to get the month when that week started (here October). Not duplicate to How to extract Month from date in R (do not have a standard date format like %Y-%m-%d). 回答1: The following will add the week-of-year to an input of year-week formatted strings and return a vector of dates as character. The lubridate