5
我有一個數據幀有三列:ref,target,distance。每個參考對同一組目標都有一個測量距離,我想得到每個參考的最小距離向量。現在我正在用for循環來做這件事,但似乎應該有一種方法來對此進行矢量化。向量化在R中的數據幀循環使用
這裏是我的代碼:
refs <- levels(data$ref)
result <- c()
for (ref in refs) {
# Find the minimum distance for observations with the current ref
# but be sure to protect against ref == target!
best_dist <- min(data[data$ref == ref & data$target != ref,]$distance)
result <- c(result, best_dist)
}
上午我通過使我的數據幀設置這樣還是有向量化一個很好的方式註定?謝謝您的幫助!
我想你一個')'缺少那裏... – adibender
ta。現在修復.. – mnel
這似乎不太正常。嘗試此操作時出現以下錯誤:** Ops.factor(ref,target)中的錯誤:級別因數集不同** 但是,** DT [,list(bestdist = min(distance)),by = ref] ** 是否產生輸出,但沒有ref!=目標檢查。 – weitzner