2017-11-25 52 views
-2

我正在處理員工數據。整個數據框有104列,但爲此,我只關心兩列。我們有一個員工編號和他們的主管(以員工編號列中存在的主管編號的形式)。我需要對數據進行排序,以便員工編號在管理員標識列中的任何實例上方。員工 - 主管排序R

下面是我的第一個解決方案,但它有幾個問題,我認爲有一個更好的方法來做到這一點。現在,它不僅僅是將行向上移動,而是添加一個新行,所以它永遠不會完成。

任何援助將不勝感激。

library(iterators) 
EmpNo <- c(1111, 1112, 1113, 1114, 1115, 1116, 1117, 1118) 
SupervisorID <- c(1118, 1117, 1114, 1112, 1112, 1118, 1117, 1117) 
supervisors <- data.frame(EmpNo, SupervisorID) 

loop <- TRUE 
while(loop) 
{ 
    loop <- FALSE 
    iSupervisor <- iter(supervisors, by ='row') 
    for(i in 1:nrow(supervisors)) 
    { 
    tempElem <- nextElem(iSupervisor) 
    if(nrow(tempElem) == 1) 
    { 
     # It does not properly move the row. 
     if(i > 1) 
     { 
     if(nrow(supervisors[tempElem$EmpNo %in% supervisors[1:(i-1),"SupervisorID"]]) > 0) 
     { 
      if(length(which(supervisors$SupervisorID == tempElem$EmpNo)) != 0) 
      { 
      sup.first <- min(which(supervisors$SupervisorID == tempElem$EmpNo)) 
      if(sup.first > i) 
      { 
       loop <- TRUE 
       if(i == nrow(supervisors)) 
       { 
       if(sup.first == 1) 
       { 
        supervisors <- rbind(supervisors[i,],supervisors[1:(i-1),]) 
       } else 
       { 
        supervisors <- rbind(supervisors[1:(sup.first-1),],supervisors[i,],supervisors[sup.first:(i-1),]) 
       } 
       } else 
       { 
       if(sup.first == 1) 
       { 
        supervisors <- rbind(supervisors[i,],supervisors[1:(i-1),], supervisors[(i+1):nrow(supervisors),]) 
       } else 
       { 
        supervisors <- rbind(supervisors[1:(sup.first-1),],supervisors[i,],supervisors[sup.first:nrow(supervisors),]) 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    if(nrow(supervisors) > 50) { loop <- FALSE } 
    } 
    rownames(supervisors) <- NULL 
} 

更新: 有一個是主管。他們的SupervisorID與他們的EmpNo相同。員工編號的排序不相關,除了它必須高於向他們報告的任何人。以下是一些示例數據。

初始化數據:

 
EmpNo SupervisorID 
1111 1118 
1112 1117 
1113 1114 
1114 1112 
1115 1112 
1116 1118 
1117 1117 
1118 1117 

期望的結果:

 
EmpNo SupervisorID 
1117 1117 
1118 1117 
1112 1117 
1111 1118 
1116 1118 
1114 1112 
1115 1112 
1113 1114 

UPDATE: 更新了代碼,使其完全可重複的,包括休息,防止其無限運行。

+2

我們可以有一個*小*可重現的例子嗎? –

+0

監事有監事嗎?在你想要的解決方案中,有沒有人可以成爲第一個?它必須是沒有監督員的人。 – G5W

+0

是的,主管可以有主管,有一個是主管。我會添加一些示例數據。 –

回答

0

下面是我的解決方案。它仍然有點慢,但它解決了這個問題。

'%!in%' <- function(x,y)!('%in%'(x,y)) 

EmpNo <- c(1111, 1112, 1113, 1114, 1115, 1116, 1117, 1118) 
SupervisorID <- c(1118, 1117, 1114, 1112, 1112, 1118, 1117, 1117) 
Status <- c('A','A','A','A','A','A','A','A') 
check <- c(1,2,3,4,5,6,7,8) 
supervisors <- data.frame(EmpNo, SupervisorID, Status, check, stringsAsFactors = FALSE) 

loop <- TRUE 
while(loop) 
{ 
    loop <- FALSE 
    supervisors$check <- apply(supervisors[,c('EmpNo', 'check', 'Status')], 1, function(y) { 
    if(y['Status'] %!in% c('T','N')){ 
     if(nrow(supervisors[y['EmpNo'] %in% supervisors[1:max(((as.numeric(y['check'])-1)),1),"SupervisorID"]]) > 0) 
     { 
     if(length(which(supervisors$SupervisorID == y['EmpNo'])) > 0) 
     { 
      sup.first <- min(which(supervisors$SupervisorID == y['EmpNo'])) 
      if(sup.first < as.numeric(y['check'])) 
      { 
      loop <<- TRUE 
      } 
      sup.first - 1 
     } else 
     { 
      nrow(supervisors) 
     } 
     } 

    } else { 
     nrow(supervisors) 
    } 
    }) 
    supervisors <- supervisors[order(supervisors$check),] 
    supervisors$check <- as.numeric(rownames(supervisors)) 
    rownames(supervisors) <- NULL 
}