2017-01-03 19 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")) 

データはすでにプロジェクトが発注され、その後、日付を。

しかし、同じ日付を持つ2つの行(行4,5と10,11など)がある場合、私はOldValueに基づいて順序を指定したいと思います。だから、私は行4よりも先に行5を、行10より先に行11を欲しいです。

どうやってこれを行うことができますか?

+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] 
関連する問題