Question: Rearranging rows of data with same value

Question

Rearranging rows of data with same value

Answers 1
Added at 2017-01-03 21:01
Tags
Question

I have the following data:

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"))

The data is already ordered by Project, then Date.

However, if there are two rows with the same Date (such as rows 4,5 and 10,11) I want to designate the order based on OldValue. So I'd like row 5 ahead of row 4, and row 11 ahead of row 10.

How can I go about doing this?

Answers to

Rearranging rows of data with same value

nr: #1 dodano: 2017-01-03 22:01
#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]
Source Show
◀ Wstecz