0

I have these columns in my dataframe, df:


library(readr)
library(dplyr)
library(lubridate)


year month day hour minute  datetime
2013 1     7   21   54      2013-01-07 21:54:00
2013 3     20  13   59      2013-03-20 13:59:00
2013 1     3   18   40      2013-01-03 18:40:00


$ year      : num [1:30115] 2013 2013 2013 2013 2013 ...
 $ month     : num [1:30115] 1 3 1 12 2 5 10 5 10 1 ...
 $ day       : num [1:30115] 7 20 3 16 24 26 30 15 31 15 ...
 $ hour      : num [1:30115] 21 13 18 13 21 8 13 7 12 13 ... 
...
 $ datetime  : POSIXct[1:30115], format: "2013-01-07 21:54:00" "2013-03-20 13:59:00" "2013-01-03 18:40:00" "2013-12-16 13:29:00" ...

I have created the datetime column with this:

dfl$datetime <- with(df, as.POSIXct(paste(year,  month,  day, hour, minute), 
                                           format = "%Y %m %d %H %M")) 

so I do not understand why when I use arrange() next

df %>% arrange(desc(datetime))

it does not sort my dataframe by datetime? I am getting

    year month   day  hour minute date_time          
   <dbl> <dbl> <dbl> <dbl>  <dbl> <dttm>             
 1  2013     1     7    21     54 2013-01-07 21:54:00
 2  2013     3    20    13     59 2013-03-20 13:59:00
 3  2013     1     3    18     40 2013-01-03 18:40:00

while it should be

2013-03-20 13:59:00
2013-01-07 21:54:00
2013-01-03 18:40:00

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Bluetail
  • 1,093
  • 2
  • 13
  • 27
  • 1
    As suggested in my comment to your other question(https://stackoverflow.com/questions/74700462/concatenate-year-month-day-and-time-problem-in-r) I'll recommend that you restart R as it seems like the current session has various problems. – harre Dec 06 '22 at 11:00
  • 1
    Alternatively, it might be a typo `dfl` instead of `df`. If so use `arrange(dfl, desc(datetime))`. – harre Dec 06 '22 at 11:01
  • I have restarted it and it is still the same, unfortunately. – Bluetail Dec 06 '22 at 11:05
  • Try reinstalling `tidyverse` and `R`. Or use the base equivalent: `df[order(df$datetime, decreasing = TRUE), ]` – harre Dec 06 '22 at 11:14

1 Answers1

0

For me it looks like it is enough to drop "df %>%" before the arrange command. I did run the following code:

library(dplyr)
library(tidyr)
library(lubridate)

df = tibble(year = c(2013,2013,2013),
        month = c(1,3,1),
        day = c(7,20,3),
        hour = c(21,13,18),
        minute = c(54,59,40))

df$datetime <- with(df, as.POSIXct(paste(year,  month,  day, hour, minute), 
                               format = "%Y %m %d %H %M")) 

arrange(df, desc(datetime))

Then I got the following output

# A tibble: 3 × 6
year month   day  hour minute datetime           
<dbl> <dbl> <dbl> <dbl>  <dbl> <dttm>             
1  2013     3    20    13     59 2013-03-20 13:59:00
2  2013     1     7    21     54 2013-01-07 21:54:00
3  2013     1     3    18     40 2013-01-03 18:40:00

Alternatively you can drop the df as an argument in arrange if you want to use the pipe for some reason.

df %>% arrange(desc(datetime))
Tor O
  • 51
  • 8