私は従業員データを扱っています。全体的なデータフレームには104個のカラムがありますが、これについては2つのカラムしか気にしません。従業員番号とそのスーパーバイザー(従業員ID列に存在するスーパーバイザーIDの形式)があります。データをソートする必要があるので、従業員番号はスーパーバイザーID列のどのインスタンスよりも上になります。従業員とスーパーバイザーのソート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
}
UPDATE: ヘッドスーパーバイザーであるものがあります。 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: 無限に実行されているから、それを維持するために休憩を含めて、それは完全に再現可能にするために、コードを更新しました。
私たちは*再現可能な例がありますか? –
監督者に監督者がいますか?あなたが望む解決策では、最初になることが許されている人はいますか?それは監督者のいない人でなければなりません。 – G5W
はい、スーパーバイザはスーパーバイザを持つことができ、トップが1つあります。いくつかのサンプルデータを追加します。 –