2017-01-03 73 views
0

,我有以下數據:重新排列的數據行有相同的值

Data <- data.frame(Project=c(123,123,123,123,123,123,124,124,124,124,124,124), 
       Date=c("12/27/2016 15:16","12/27/2016 15:20","12/27/2016 15:24","12/27/2016 15:28","12/27/2016 15:28","12/27/2016 15:42","12/28/2016 7:22","12/28/2016 7:26","12/28/2016 7:35","12/28/2016 11:02","12/28/2016 11:02","12/28/2016 11:28"), 
       OldValue=c("","Open","In Progress","Open","System Declined","In Progress","System Declined","Open","In Progress","Open","Complete","In Progress"), 
       NewValue=c("Open","In Progress","System Declined","In Progress","Open","System Declined","Open","In Progress","Complete","In Progress","Open","Complete")) 

的數據已經被訂購項目,然後日期。

但是,如果有兩行具有相同的Date(例如4,5和10,11行),我想指定基於OldValue的順序。所以我想第5排在第4排之前,第11排在第10排之前。

我該如何去做這件事?

+0

按字母順序排序會產生不同的結果,如果'OldValue'是一個因素。 –

+0

我試着按Project,Date,OldValue和大部分工作來訂購我的數據。然而,在上面的例子中,有些情況下不起作用,即4,5行 – Dfeld

回答

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]