How can I sort POSIXct datetime format in R?

Alex

I have a data frame with 4 columns, the first of which is called Date_et_heure. In that column, I have a POSIXct POSIXt class datetimes with format "%Y-%m-%d %H:%M:%S". I would like to arrange my dataframe so that the rows are always chronological.

I tried to use the arrange() function but it cannot accept POSIXct POSIXt format data; I always get the error:

Error in UseMethod("arrange") : no applicable method for 'arrange' applied to an object of class "c('POSIXct', 'POSIXt')

I tried to use the order() function, for which I needed to transform POSIXct with the as.Date() function. But as.Date() ignores the time ("%H:%M:%S") format of the column.

Does anyone know if there is a method to order POSIXct class data? Hopefully with a conversion that is reliable.

Thank you!

Merijn van Tilborg

Post your code, the error message does not indicate the issue is your object with the class mentioned, but that you provided a non applicable method for the object that happen to have that class in this case.

The issue lays not in the dplyr functionality as seen also in the examples in other replies.

Here an example of both POSIXlt and POSIXct (which both have the class "POSIXct" "POSIXt"). You can sort on both, and both ways.

df <- data.frame(
  Date_et_heurePOSIXct = sample(seq(as.POSIXct('2021-08-01'), as.POSIXct('2021-11-09', tz = "UTC"), by = "1 sec"), 5),
  Date_et_heurePOSIXlt = sample(seq(as.POSIXlt('2021-08-01'), as.POSIXlt('2021-11-09', tz = "UTC"), by = "1 sec"), 5)
)

df %>% arrange(Date_et_heurePOSIXct)
df %>% arrange(desc(Date_et_heurePOSIXct))
df %>% arrange(Date_et_heurePOSIXlt)
df %>% arrange(desc(Date_et_heurePOSIXlt))


class(df$Date_et_heurePOSIXct)
class(df$Date_et_heurePOSIXlt)

Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.

En caso de infracción, por favor [email protected] Eliminar

Editado en
0

Déjame decir algunas palabras

0Comentarios
Iniciar sesiónRevisión de participación posterior

Artículos relacionados

Lubridate неправильно конвертирует datetime в POSIXct в R (дд / мм / гг чч: мм: сс)

How can I change format datetime to date in vue component?

"String was not recognized as a valid DateTime." format "yyyy-MM-dd" how i can use this format

How can I increment a date in datetime64[ns] format by 6 calendar months?

How can I sort / format my HTML input, with their titles in a row, at the middle of my page?

How to convert datetime format into 'ddmmyyyy' using R?

How can I use datetime.strptime on a string in the format '0000-00-00T00:00:00+00:00'?

how can I sort a field form an Endnote Export File format where the Line contains GRAZ in the address as first line?

How can I create an optional DateTime parameter?

How can I sort an array of weekday strings?

How can I cancel Collections.sort()

How can i resort this json to sort on price?

Prolog How can I sort a list like this

How can I remove duplicate objects and sort?

How can I sort this according to the length of the output?

How can I subtract a datetime.now object with a datetime string?

append a datetime object on POSIXct

How can I verify an input parameter format?

How to format datetime like java?

How to convert a dataframe into datetime format

How can I convert DateTime to String in Linq Query?

How can i add 1 day to a DateTime variable in Visual Basic?

How can I replace datetime with While loop in Python

How can I sort a list of numerical strings? Preferably using LINQ

How can I sort the values in a custom Keras / Tensorflow Loss Function?

How can I group and sort columns in spark.rdd

How can I sort a vector by its computed values?

How can I sort a fixed-length array in golang?

How can I sort results by aggregate in Cloudwatch Log Insights?

TOP Lista

CalienteEtiquetas

Archivo