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
19
votes
3 answers

Modifying timezone of a POSIXct object without changing the display

I have a POSIXct object and would like to change it's tz attribute WITHOUT R to interpret it (interpret it would mean to change how the datetime is displayed on the screen). Some background: I am using the fasttime package from S.Urbanek, which…
statquant
  • 13,672
  • 21
  • 91
  • 162
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
  • 12
17
votes
1 answer

rounding times to the nearest hour in R

I have data in the format time <- c("16:53", "10:57", "11:58") etc I would like to create a new column where each of these times is rounded to the nearest hour. I cannot seem to get the POSIX command to work for me. …
user1558387
  • 195
  • 1
  • 2
  • 7
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
16
votes
3 answers

Subset observations that differ by at least 30 minutes time

I have a data.table (~30 million rows) consisting of a datetime column in POSIXct format, an id column and a few other columns (in the example, I just left one irrelevant column x to demonstrate that there are other columns present that need to be…
talat
  • 68,970
  • 21
  • 126
  • 157
16
votes
2 answers

How to initialize data.frame with column of type POSIXct?

I can initialize a data.frame via df <- data.frame(a=numeric(), b=character()) But how do I define a column of type POSIXct? df <- data.frame(a=numeric(), b=character(), c=POSIXct()) won't work.
JerryWho
  • 3,060
  • 6
  • 27
  • 49
16
votes
4 answers

Efficient comparison of POSIXct in data.table

Hello I am looking for an efficient way of selecting POSIXct rows from a data.table such that the time of day is less than say 12:00:00 (NOTE that millisecond is NOT required, so we can use ITime for example) set.seed(1); N = 1e7; DT =…
statquant
  • 13,672
  • 21
  • 91
  • 162
14
votes
2 answers

R: as.POSIXct timezone and scale_x_datetime issues in my dataset

I spent some time trying to figure out why the hour ticks were shifted when scale_x_datetime was applied. I've tried to give the timezone when the Date/Time column was created. I used ggplot and scale_x_datetime() from the package scales. The hour…
Kuo-Hsien Chang
  • 925
  • 17
  • 40
14
votes
2 answers

Convert Factor to Date/Time in R

This is the information contained within my dataframe: ## minuteofday: factor w/ 89501 levels "2013-06-01 08:07:00",... ## dDdt: num 7.8564 2.318 ... ## minutes: POSIXlt, format: NA NA NA I need to convert the minute of day column to a date/time…
Michelle
  • 193
  • 1
  • 1
  • 6
14
votes
3 answers

R - converting date and time fields to POSIXct with HHMMSS format

I have a data file which has three columns thus: 20010101 000000 0.833 20010101 000500 0.814 20010101 001000 0.794 20010101 001500 0.772 ... As is fairly clear to human eyes, the first two are date and time. I need to convert them into a…
Flyto
  • 676
  • 1
  • 7
  • 18
13
votes
4 answers

as.POSIXct gives an unexpected timezone

I'm trying to convert a yearmon date (from the zoo package) to a POSIXct in the UTC timezone. This is what I tried to do: > as.POSIXct(as.yearmon("2010-01-01"), tz="UTC") [1] "2010-01-01 01:00:00 CET" I get the same when I convert a Date: >…
SiggyF
  • 22,088
  • 8
  • 43
  • 57
13
votes
6 answers

Calculate part of duration that occur in each hour of day

I have a dataframe with start and end times: id start_time end_time 1 1 2018-09-02 11:13:00 2018-09-02 11:54:00 2 2 2018-09-02 14:34:00 2018-09-02 14:37:00 3 3 2018-09-02 03:00:00 2018-09-02 03:30:00 4 4 2018-09-02 03:49:00…
DanG
  • 689
  • 1
  • 16
  • 39
13
votes
1 answer

Subset dataframe based on POSIXct date and time greater than datetime using dplyr

I am not sure what is going wrong with selecting date times as a POSIXct format. I have read several comments on subsetting a dataframe based on as.Date and I can get that to work without an issue. I have also read many posts suggesting that…
Bill Perry
  • 463
  • 1
  • 5
  • 13
13
votes
2 answers

Join two data frames in R based on closest timestamp

Hi I have two tables (table1 and table2 below) and would like to join them based on the closest timestamp to form expected_output. Some kind of solution involving dplyr would be great if possible, but not if it further complicates things. table1 =…
John
  • 802
  • 2
  • 9
  • 19
13
votes
2 answers

Speedup conversion of 2 million rows of date strings to POSIX.ct

I have a csv which includes about 2 million rows of date strings in the format: 2012/11/13 21:10:00 Lets call that csv$Date.and.Time I want to convert these dates (and their accompanying data) to xts as fast as possible I have written a script…
n.e.w
  • 1,128
  • 10
  • 23
1
2
3
70 71