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

데이터가 이미 프로젝트에 의해 정렬 된 다음 날짜.

그러나 동일한 날짜 (예 : 행 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]