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!
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
Déjame decir algunas palabras