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:

1056 questions
95
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
  • 50,859
  • 17
  • 87
  • 112
76
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
  • 9,610
  • 12
  • 49
  • 78
65
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
  • 781
  • 1
  • 6
  • 4
52
votes
7 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…
Suraj
  • 35,905
  • 47
  • 139
  • 250
39
votes
3 answers

Line break when no data in ggplot2

I am using R to plot some data. Date <- c("07/12/2012 05:00:00", "07/12/2012 06:00:00", "07/12/2012 07:00:00", "07/12/2012 08:00:00","07/12/2012 10:00:00","07/12/2012 11:00:00") Date <- strptime(Date, "%d/%m/%Y %H:%M") Counts <-…
KT_1
  • 8,194
  • 15
  • 56
  • 68
39
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
  • 12,762
  • 7
  • 71
  • 120
34
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
  • 985
  • 2
  • 14
  • 28
33
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
  • 29,791
  • 35
  • 142
  • 201
32
votes
7 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
  • 8,598
  • 26
  • 103
  • 167
27
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
  • 281
  • 1
  • 3
  • 3
26
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
  • 269
  • 1
  • 3
  • 4
26
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
  • 8,598
  • 26
  • 103
  • 167
26
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
  • 261
  • 1
  • 3
  • 3
22
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.
  • 17,826
  • 11
  • 69
  • 103
20
votes
1 answer

Convert Date to POSIXct

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"
Carlos Cinelli
  • 11,354
  • 9
  • 43
  • 66
1
2 3
70 71