Questions tagged [posixct]

In the R language, the classes "POSIXct" and "POSIXlt" are representing calendar dates and times (to the nearest second).

The classes POSIXct and POSIXlt are representing calendar dates and times in R.

The class POSIXct represents the (signed) number of seconds since the beginning of 1970 (in the UTC timezone) as a numeric vector.

Class POSIXlt is a named list of vectors representing

  • sec: seconds (0-61)
  • min: minutes (0-59)
  • hour: hours (0-23)
  • mday: day of the month (1-31)
  • mon: months after the first of the year (0-11)
  • year: years since 1900
  • wday: day of the week, starting on Sunday (0-6)
  • yday: day of the year (0-365)
  • isdst: Daylight Savings Time flag. Positive if in force, zero if not, negative if unknown.

POSIXct is more convenient for including in data frames, and POSIXlt is closer to human-readable forms. A virtual class POSIXt exists from which both of the classes inherit.

References:

894 questions
86
votes
7 answers

Extracting time from POSIXct

How would I extract the time from a series of POSIXct objects discarding the date part? For instance, I have: times <- structure(c(1331086009.50098, 1331091427.42461, 1331252565.99979, 1331252675.81601, 1331262597.72474, 1331262641.11786,…
nico
  • 48,112
  • 17
  • 80
  • 111
61
votes
3 answers

Adding time to POSIXct object in R

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…
BlueTrin
  • 8,342
  • 8
  • 44
  • 70
58
votes
2 answers

How R formats POSIXct with fractional seconds

I believe that R incorrectly formats POSIXct types with fractional seconds. I submitted this via R-bugs as an enhancement request and got brushed off with "we think the current behavior is correct -- bug deleted." While I am very appreciative of the…
Robert Almgren
  • 711
  • 1
  • 6
  • 4
50
votes
6 answers

Looping over a Date or POSIXct object results in a numeric iterator

Why does iterating through a Date or POSIXct object result in numeric? For example: test = as.Date("2009-01-01") print( class( test ) ) # [1] "Date" for ( day in test ) { print( class( day ) ) } # [1] "numeric" The same thing happens with…
SFun28
  • 32,209
  • 43
  • 123
  • 233
33
votes
3 answers

Round a POSIX date (POSIXct) with base R functionality

I'm currently playing around a lot with dates and times for a package I'm building. Stumbling across this post reminded me again that it's generally not a bad idea to check out if something can be done with basic R features before turning to contrib…
Rappster
  • 11,680
  • 7
  • 58
  • 113
29
votes
1 answer

Valid time zones in lubridate

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…
emagar
  • 813
  • 1
  • 11
  • 23
28
votes
3 answers

Reliable way to detect if a column in a data.frame is.POSIXct

R has is.vector, is.list, is.integer, is.double, is.numeric, is.factor, is.character, etc. Why is there no is.POSIXct, is.POSIXlt or is.Date? I need a reliable way to detect POSIXct object, and class(x)[1] == "POSIXct" seems really... dirty.
Zach
  • 27,553
  • 31
  • 130
  • 193
27
votes
6 answers

How to make an empty vector of POSIXct

I want to make an empty vector of POSIXct so that I can put a POSIXct in it: vec <- vector("POSIXct", 10) vec vec[1] <- "2014-10-27 18:11:36 PDT" vec That does not work. Any ideas?
user3022875
  • 7,420
  • 21
  • 80
  • 145
23
votes
2 answers

How to calculate time difference with previous row of a data.frame by group

The problem I am trying to solve is that I have a data frame with a sorted POSIXct variable in it. Each row is categorized and I want to get the time differences between each row for each level and add that data back into a new variable. The…
Mntester
  • 239
  • 1
  • 2
  • 4
22
votes
1 answer

converting datetime string to POSIXct date/time format in R

Consider a string in the format test <- "YYYY-MM-DDT00:00:00.000-08:00" My goal is to convert those strings to POSIXct format so that I can plot the data. my initial thought was to use as.POSIXct(test) ...but that seems to truncate the datetime…
CFrench
  • 231
  • 1
  • 2
  • 3
21
votes
1 answer

How to make time difference in same units when subtracting POSIXct

I want to subtract to POSIXct. I can do this but depending on the first row (i guess?) the difference will be in seconds or minutes. Below you can see the first diff is in seconds and the second diff is in minutes because I changed the time…
user3022875
  • 7,420
  • 21
  • 80
  • 145
20
votes
1 answer

Milliseconds in POSIXct Class

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…
user3355131
  • 201
  • 1
  • 2
  • 3
19
votes
6 answers

Create a regular sequence of date-times (POSIXct) using seq()

My goal is to create a vector of POSIXct time stamps given a start, an end and a delta (15min, 1hour, 1day). I hoped I could use seq for this, but I have a problem converting between the numeric and POSIXct representation: now <- Sys.time() now #…
Karsten W.
  • 15,931
  • 10
  • 60
  • 93
17
votes
4 answers

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

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…
sunt
  • 303
  • 2
  • 5
  • 11
17
votes
1 answer

Character POSIXct Conversion in R causes wrong timezone values on daylight saving time transition (CEST/CET))

I have a problem converting POSIXct to character and back in POSIXct in R. I run the following code: time_seq_01 <- seq(as.POSIXct("2012-10-28 02:00:00"), by = 900, length.out = 10) time_seq_02 <- as.character(time_seq_01) time_seq_03 <-…
kajo
  • 181
  • 1
  • 6
1
2 3
59 60