重新排列具有相同值的数据行

时间:2017-01-03 20:43:56

标签: r sorting

我有以下数据:

CAId

数据已按项目排序,然后按日期排序。

但是,如果有两行具有相同的日期(例如行4,5和10,11),我想根据OldValue指定顺序。所以我喜欢第4行之前的第5行和第10行之前的第11行。

我该怎么做呢?

1 个答案:

答案 0 :(得分:2)

#Assign Desired order to the OldValue, CHANGE "y" IF NECESSARY
OldValue_order = data.frame(OldValue = c("","Open","In Progress","System Declined","Complete"), y = c(0,4,2,1,3))

# We'll need lookup command to copy desired order to the "Data"
library(qdapTools)
Data$OV_order = lookup(Data$OldValue, OldValue_order) # Adds new column to "Data"

# Arrange the data.frame in desired order
Data = Data[with(Data, order(Project, as.POSIXct(Date, format = "%m/%d/%Y %H:%M"), OV_order)),]

#Remove the added column
Data = Data[1:4]