2016-12-31 17 views
-5
STOP_ID STP1 STP2 STP3 STP4 STP5 STP6 
DEP1 10  518  497  131  16  131 
DEP2 510  22  32  452  510  452 
DEP3 487  27  45  439  487  430 
DEP4 132  466 445  32  132  62 
DEP5 43  518 487  131  55  483 
DEP6 132  466 445  32  132  25 

我期待 rowname:DEP1,列名:STP1最小值:10;在數據幀(尺寸195 X 195),在一排相應列的最低值相關的行名和列名

人幫助我得到上述答案

+0

看看'which.min()' –

+0

我得到從給定的行 – Keerthy

+0

但我想rowname即DEP1相應的列名即STP1然後最小值最小值,最小值10 – Keerthy

回答

1
apply(df[2:7], 1, function(x) colnames(df[2:7])[which.min(x)]) 
# [1] "STP1" "STP2" "STP2" "STP4" "STP1" "STP6" 
apply(df[2:7], 1, function(x) x[which.min(x)]) 
# [1] 10 22 27 32 43 25 

將這些上述向量添加到新列,如果這是你在找什麼。

x = apply(df[2:7], 1, function(x) colnames(df[2:7])[which.min(x)]) 
y = apply(df[2:7], 1, function(x) x[which.min(x)]) 
df$min_vale = y 
df$col_name = x 

# df 
# STOP_ID STP1 STP2 STP3 STP4 STP5 STP6 min_vale col_name 
#1 DEP1 10 518 497 131 16 131  10  STP1 
#2 DEP2 510 22 32 452 510 452  22  STP2 
#3 DEP3 487 27 45 439 487 430  27  STP2 
#4 DEP4 132 466 445 32 132 62  32  STP4 
#5 DEP5 43 518 487 131 55 483  43  STP1 
#6 DEP6 132 466 445 32 132 25  25  STP6 
使用 dplyrtidyr另一種方法

使用data.table

library(data.table) 
df1 = setDT(df)[, apply(.SD, 1, function(x) list(colnames(df[2:7])[which.min(x)], x[which.min(x)])), 
      .SDcols = colnames(df)[2:7]] 
colnames(df1) = df$STOP_ID 
# df1 
#  DEP1 DEP2 DEP3 DEP4 DEP5 DEP6 
#1: STOP_ID STP1 STP1 STP3 STOP_ID STP5 
#2:  10 22 27 32  43 25 
+0

但I W螞蟻rowname即DEP1,和相應的列名即STP1然後最小值的最小值10。第二行:rowname DEP2;列名稱:STP2和值22;第三行:rowname DEP3列名:STP2和值27 ... – Keerthy

+0

這是你在上面得到什麼。看看第一列和最後2列。您可以通過分配NULL來刪除不需要的列NULL –

+0

我需要將所有6個站點的最小值進行聚類,這就是爲什麼我需要檢查列名然後創建6個數組然後將值推送到相應數組(六個STP1到6之中) – Keerthy

1

這裏的另一種方法,

library(dplyr) 
library(tidyr) 

df %>% 
    gather(c.names, value, STP1:STP6) %>% 
    group_by(STOP_ID) %>% 
    filter(value == min(value)) %>% 
    arrange(value) %>% 
    rename(r.names = STOP_ID) 

#Source: local data frame [6 x 3] 
#Groups: r.names [6] 

# r.names c.names value 
# <fctr> <chr> <int> 
#1 DEP1 STP1 10 
#2 DEP2 STP2 22 
#3 DEP6 STP6 25 
#4 DEP3 STP2 27 
#5 DEP4 STP4 32 
#6 DEP5 STP1 43 
2

我們可以做max.col

j1 <- max.col(-df1[-1], "first") 
res <- transform(df1, min_val = df1[-1][cbind(1:nrow(df1), j1)], 
              col_name = names(df1)[-1][i1]) 
res 
# STOP_ID STP1 STP2 STP3 STP4 STP5 STP6 min_val col_name 
#1 DEP1 10 518 497 131 16 131  10  STP1 
#2 DEP2 510 22 32 452 510 452  22  STP2 
#3 DEP3 487 27 45 439 487 430  27  STP2 
#4 DEP4 132 466 445 32 132 62  32  STP4 
#5 DEP5 43 518 487 131 55 483  43  STP1 
#6 DEP6 132 466 445 32 132 25  25  STP6 
這個使用矢量選項

或者使用data.table

library(data.table) 
setDT(df1)[melt(setDT(df1), id.var = "STOP_ID", variable.name = "col_name", 
     value.name = "min_val")[, .SD[which.min(min_val)], STOP_ID] 
      , on = "STOP_ID"] 
# STOP_ID STP1 STP2 STP3 STP4 STP5 STP6 col_name min_val 
#1: DEP1 10 518 497 131 16 131  STP1  10 
#2: DEP2 510 22 32 452 510 452  STP2  22 
#3: DEP3 487 27 45 439 487 430  STP2  27 
#4: DEP4 132 466 445 32 132 62  STP4  32 
#5: DEP5 43 518 487 131 55 483  STP1  43 
#6: DEP6 132 466 445 32 132 25  STP6  25 

如果我們只需要 'STOP_ID', 'MIN_VAL' 和 'COL_NAME'

melt(setDT(df1), id.var = "STOP_ID", variable.name = "col_name", 
      value.name = "min_val")[, .SD[which.min(min_val)], STOP_ID] 
# STOP_ID col_name min_val 
#1: DEP1  STP1  10 
#2: DEP2  STP2  22 
#3: DEP3  STP2  27 
#4: DEP4  STP4  32 
#5: DEP5  STP1  43 
#6: DEP6  STP6  25 
相關問題