r - 基于多个条件的向量中的最小值

时间:2017-09-18 21:16:46

标签: r multiple-columns min

我想添加一列Min/NotMin,用于标识列LCPORG NAMEInquiry的最低ARNR。它应该排除0和NA,除非它是唯一的选择。

例如,它应如下所示:

ORG NAME    Inquiry ARNR    Quantity    LCP     COGS        Min/NotMin
BCA         22245   1001167 304         31.621  96.12784    NotMin
BCA         22245   1001167 304         37.675  114.532     NotMin
BCA         22245   1001167 304         0       0           NotMin
BCA         22245   1001167 304         NA      0           NotMin
BCA         22245   1001167 304         0       0           NotMin
BCA         22245   1001167 304         144.915 440.5416    NotMin
BCA         22245   1001167 304         NA      0           NotMin
BCA         22245   1001167 304         NA      0           NotMin
BCA         22245   1001167 304         39.381  119.71824   NotMin
BCA         22245   1001167 304         27.014  82.12256    Min
BIP         22188   1003771 100         1.394   1.394       NotMin
BIP         21729   1004077 236278      0       0           NotMin
BIP         21729   1004077 236278      NA      0           NotMin
BIP         21729   1004077 236278      1.52    3591.4256   NotMin
BIP         21729   1004077 236278      0       0           NotMin
BIP         21729   1004077 236278      NA      0           NotMin
BIP         21729   1004077 236278      3.848   9091.97744  NotMin
BIP         21729   1004077 236278      0.713   1684.66214  Min
BIP         21729   1004077 236278      1.837   4340.42686  NotMin
BIP         21729   1004077 236278      0       0           NotMin
BCA         21777   1004271 3000        3.057   91.71       Min
BCA         21777   1004271 3000        0       0           NotMin
BCA         21777   1004271 3000        3.848   115.44      NotMin
BIP         21981   1004387 100         0       0           Min
BCA         22204   1008463 125000      12.636  15795       NotMin
BCA         22204   1008463 125000      8.67    10837.5     Min
BCA         21732   1008463 125000      13.321  16651.25    NotMin
BCA         21732   1008463 125000      0       0           NotMin
BCA         21732   1008463 125000      8.034   10042.5     Min
BCA         21732   1008463 125000      NA      0           NotMin
希望你能帮忙!谢谢!

1 个答案:

答案 0 :(得分:1)

这是一个解决方案。我使用data.tablewhich函数来执行此操作。

# Assuming DF is your input data frame
# Replace 0 with NA since you would like to exclude 0's
DF$LCP[DF$LCP == 0] <- NA  

# Creating a Min/NotMin column with default value 'NotMin'
DF$MinOrNotMin <- "NotMin" 

# Getting minimum by ORG NAME, Inquiry, ARNR and their index
library(data.table)
DT <- data.table(DF)
MinDF <- DT[, .SD[which.min(LCP),], by = list(ORG_NAME, Inquiry, ARNR)]
index_DF <- which( outer(DF$ORG_NAME, MinDF$ORG_NAME, "==") & 
                 outer(DF$Inquiry, MinDF$Inquiry, "==") & 
                 outer(DF$ARNR, MinDF$ARNR, "==") & outer(DF$LCP, MinDF$LCP, "==") , 
               arr.ind=TRUE)

# Writing "Min" in Min/NotMin column for minimum value
DF$MinOrNotMin[index_DF[,1]] <- "Min"

## Your final Output 
## DF
ORG_NAME Inquiry  ARNR     Quantity   LCP        COGS     MinOrNotMin
   BCA   22245   1001167      304  31.621    96.12784      NotMin
   BCA   22245   1001167      304  37.675   114.53200      NotMin
   BCA   22245   1001167      304      NA     0.00000      NotMin
   BCA   22245   1001167      304      NA     0.00000      NotMin
   BCA   22245   1001167      304      NA     0.00000      NotMin
   BCA   22245   1001167      304 144.915   440.54160      NotMin
   BCA   22245   1001167      304      NA     0.00000      NotMin
   BCA   22245   1001167      304      NA     0.00000      NotMin
   BCA   22245   1001167      304  39.381   119.71824      NotMin
   BCA   22245   1001167      304  27.014    82.12256         Min
   BIP   22188   1003771      100   1.394     1.39400         Min
   BIP   21729   1004077   236278      NA     0.00000      NotMin
   BIP   21729   1004077   236278      NA     0.00000      NotMin
   BIP   21729   1004077   236278   1.520  3591.42560      NotMin
   BIP   21729   1004077   236278      NA     0.00000      NotMin
   BIP   21729   1004077   236278      NA     0.00000      NotMin
   BIP   21729   1004077   236278   3.848  9091.97744      NotMin
   BIP   21729   1004077   236278   0.713  1684.66214         Min
   BIP   21729   1004077   236278   1.837  4340.42686      NotMin
   BIP   21729   1004077   236278      NA     0.00000      NotMin
   BCA   21777   1004271     3000   3.057    91.71000         Min
   BCA   21777   1004271     3000      NA     0.00000      NotMin
   BCA   21777   1004271     3000   3.848   115.44000      NotMin
   BIP   21981   1004387      100      NA     0.00000      NotMin
   BCA   22204   1008463   125000  12.636 15795.00000      NotMin
   BCA   22204   1008463   125000   8.670 10837.50000         Min
   BCA   21732   1008463   125000  13.321 16651.25000      NotMin
   BCA   21732   1008463   125000      NA     0.00000      NotMin
   BCA   21732   1008463   125000   8.034 10042.50000         Min
   BCA   21732   1008463   125000      NA     0.00000      NotMin

P.S。第12行BIP 22188 1003771 100 1.394 1.394 NotMin需要最小,因为列ORG NAMEInquiryARNR只有一行。