问题
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:
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.
来源:https://stackoverflow.com/questions/54296430/arrange-function-conflict-between-lubridate-and-dplyr-r