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

"character string is not in a standard unambiguous format" when data is numeric

I have data in the form of minutes after midnight. For example, "70" corresponds to 1:10am. I am trying to convert it to HH:mm in R so that I can do some time series analyses. Here is the code I have tried: xtime <-…
Joann
  • 33
  • 3
3
votes
1 answer

Parse string with a 5-digit year to POSIXct

I want to parse a character with a 5-digit (or more) year, e.g. 10000-01-01 01:00:00 to class POSIXct. A 4-digit year is of course no problem. For example, as.POSIXct("1000-01-01 01:00:00") But with a 5-digit year, e.g. 10000, as.POSIXct…
WCMC
  • 1,602
  • 3
  • 20
  • 32
3
votes
4 answers

How to copy rows by POSIXct interval and past it on top of the same dataframe?

My dataframe looks like this: dput(head(sAUR,50)) structure(list(date = structure(c(1475315922, 1475660422, 1475920222, 1476179992, 1476524422, 1476784232, 1477044062, 1477388482, 1477648302, 1477911722, 1478256142, 1478515962, 1478775782,…
Cláudio Siva
  • 502
  • 2
  • 10
3
votes
2 answers

R: Compute number of rows within time interval

let's assume the following dataframe: df <- tibble(ID = c(12, 12, 12, 13, 13, 13), times = c(as.POSIXct("2021-01-02 10:00:00"), as.POSIXct("2021-01-02 11:00:00"), as.POSIXct("2021-01-02 13:00:00"), …
Ai4l2s
  • 525
  • 2
  • 9
3
votes
2 answers

Difference between "as.numeric" and "as.POSIXct.numeric" (only the first seems to work)?

I am struggling with as.POSIXct.numeric. The following code works: dates <- structure(c(1649285787, 1651134684), class = c("POSIXct", "POSIXt"), tzone = "UTC") lims <- as.numeric(dates) limits <- as.POSIXct.numeric(lims, origin =…
Christoph
  • 6,841
  • 4
  • 37
  • 89
3
votes
0 answers

Is as.vector() for POSIXct objects buggy?

The funny part Let's start with the set-up (R console): > z <- Sys.time() > z [1] "2022-03-15 00:21:42 CET" > class(z) [1] "POSIXct" "POSIXt" Now, the plot (as in joke, not graphics): > as.numeric(z) [1] 1647300102 > as.character(z) [1] "2022-03-15…
user51187286016
  • 256
  • 1
  • 5
3
votes
2 answers

Plot timestamps showing milliseconds with Plotly in R

I'm trying to plot a time series with Plotly which includes milliseconds. However, Plotly rounds each timestamp to the nearest second when displaying the data. I would like the data to be plotted so that the points are evenly spaced along the x-axis…
Muon
  • 1,294
  • 1
  • 9
  • 31
3
votes
0 answers

data.table median on a 'POSIXct' column returns 'numeric' when 'by =' is specified

It seems POSIXct class is not retained when x[, median(datetime) , by = group] although x[, median(datetime) ] returns a "POSIXct". Calling as.POSIXct returns the desired output though. Is there a reason I need to call as.POSIXct…
MihaiV
  • 148
  • 8
3
votes
3 answers

Get the month from the week of the year

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…
Andrei Niță
  • 517
  • 1
  • 3
  • 14
3
votes
1 answer

R as.POSIXct parsing error

I am trying to parse a vector of time string and came across a strange error. For example, if I run the following section of code, R returned the result as expected. time_format="%m/%d/%Y %H:%M:%S" t_1 = "03/13/2011 01:00:10" as.POSIXct(t_1, format…
defoo
  • 5,159
  • 11
  • 34
  • 39
3
votes
1 answer

Converting selected columns from POSIXct to Date with particular format using apply function

Occasionally, I read in data from Microsoft Excel into R. In Excel, the date variables are formatted correctly (e.g. 31-Dec-2017). Following reading into R, the same date variable gets converted into another format (e.g. 2017-12-31). A sample of my…
DTYK
  • 1,098
  • 1
  • 8
  • 33
3
votes
1 answer

R: rounding time to the nearest hour

I have a vector with time (from an Excel file). It gets read in as a factor. This is a short version/example of it: starttime <- factor(c("12:55:00", "13:45:00", "14:30:00", "10:00:00", "10:40:00", "12:00:00", "12:30:00")) I wanted to round all…
Geraldine
  • 771
  • 3
  • 9
  • 23
3
votes
2 answers

How to convert a date as year/week to the first day of the week via POSIXct?

I want to convert myDate=as.character("2017/02") which is in the format year/week to a date that is the first day of this week. So I tried: print(as.POSIXct(myDate,format="%Y/%U")) However, this gives me [1] "2017-05-03 CEST" which is certainly…
nexonvantec
  • 572
  • 1
  • 5
  • 18
3
votes
1 answer

R Passing a string vector through tz in POSIXct function

I have two vectors that I am trying to pass through a POSIXct(). GMTDteTmeStamp is my list of times in GMT and TZ is my list of corresponding local timezones that I need the GMT times translated into. I have tried multiple ways of passing TZ…
Michael Byars
  • 117
  • 2
  • 13
3
votes
2 answers

R: Convert Sys.now() to epoch timestamp with microseconds

I want to convert current time to epoch timestamp with microseconds in R. For that I am running following code. options(digits.secs = 6) now = Sys.time() as.double(now) When I simply print now the output is "2017-11-11 20:40:18.370305 CET"…
Chadwick Robbert
  • 1,026
  • 1
  • 11
  • 23