0

I want to rearrange a df with the function arrange from dplyr. The problem is that seems that these two packages have a conflict when I have another column (let's say v2) distinct from the column I want to arrange the df:

   library(dplyr)
library(lubridate)

start_dates <- as.Date(1:10, origin = "2018-01-01")
my_df <- data.frame(x = 10:1, y = interval(start_dates, start_dates + 1), 
                    z=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"))

my_df %>% arrange(z)

Error in arrange_impl(.data, dots) : Column y classes Period and Interval from lubridate are currently not supported.

How I can order my dataframe? Thank you in advanced.

1 Answers1

3

I wouldn't call this a "conflict" per se, just one package not supporting the features of another.

If dplyr's arrange() doesn't support Interval classes, just use order() like normal:

library(dplyr)
library(lubridate)

start_dates <- as.Date(1:10, origin = "2018-01-01")
my_df <- data.frame(x = 10:1, y = interval(start_dates, start_dates + 1))

my_df %>% arrange(y)

Error in arrange_impl(.data, dots) :
Column `y` classes Period and Interval from lubridate are currently not supported.

my_df[order(my_df$y), ]

#     x                              y
# 1  10 2018-01-02 UTC--2018-01-03 UTC
# 2   9 2018-01-03 UTC--2018-01-04 UTC
# 3   8 2018-01-04 UTC--2018-01-05 UTC
# 4   7 2018-01-05 UTC--2018-01-06 UTC
# 5   6 2018-01-06 UTC--2018-01-07 UTC
# 6   5 2018-01-07 UTC--2018-01-08 UTC
# 7   4 2018-01-08 UTC--2018-01-09 UTC
# 8   3 2018-01-09 UTC--2018-01-10 UTC
# 9   2 2018-01-10 UTC--2018-01-11 UTC
# 10  1 2018-01-11 UTC--2018-01-12 UTC

Note

For future reference, I would take seriously the admonition by OTStats -- often without example data it is very difficult for others to help you. I would check out How to Make a Great R Reproducible Example.

duckmayr
  • 16,303
  • 3
  • 35
  • 53
  • I've edited the original post with your example in order to clarify. I have one more question: Does ´order()´ accept more than one variable? – Armando González Díaz Jan 21 '19 at 19:37
  • 1
    @ArmandoGonzálezDíaz Sure, you could do something like `foo[order(foo$bar, foo$baz), ]`. Check out `help("order")` for more details -- it's first argument is `...`, which accepts an arbitrary sequence of variables to order by – duckmayr Jan 21 '19 at 19:39
  • I've checked but it not works in the same way that arrange. Don't rename the columns – Armando González Díaz Jan 21 '19 at 20:33
  • order shows the df in the right order, but when i made operations based in order i.e. `my_df$rank<-dplyr::ntile(my_df$z,30)` the operation is has not the same result when is ordered witharrange. furthermore, colnames is not replaced in order to be consecuent with new order – Armando González Díaz Jan 22 '19 at 00:04