2016-05-16 120 views
1

我有一個有7列10000行的矩陣。第一列是我的reference列,因此我想遍歷每一行,並且想要將列2至7中的值與該特定行的reference列中的值進行比較,並選擇最接近reference中值的列柱。如果沒有很多for/if的話,怎麼可能有效地做到這一點?找到R中每行最接近第一列值的值

structure(c(5.1, 5.9, 5.4, 5.3, 5, 5.4, 5.3, 5.2, 5.6, 5.4, 5.02170584053232, 
4.7707877401276, 5.06375474838051, 5.62339034738835, 5.03818760112784, 
4.93262233804147, 5.01363587188261, 5.20762340416899, 5.01142387340824, 
4.87441004381736, 4.9885999770401, 5.10594649539864, 4.91373678035632, 
6.40014673007642, 5.36585348353235, 4.93331644076153, 4.97655907515639, 
5.03655809299243, 4.98817001210049, 5.07459010033282, 5.01177736675976, 
5.51490826667958, 4.86182076993167, 5.57009612211833, 4.94165243270682, 
5.04446822036351, 5.36173682507477, 5.01024350590995, 5.08292900978033, 
5.16129105727075, 4.96815931588898, 5.41090215270089, 5.0259411829201, 
5.44071363199116, 4.9755685617346, 4.94789916779388, 5.00420130774358, 
5.08710969184263, 4.051, 4.85172231763127, 5.08267286369209, 
5.50824034854424, 4.98865830869784, 6.46969360152923, 5.46579720589366, 
5.0600279422611, 5.19608698551803, 5.05996715141585, 4.93119298095202, 
5.00401343752039, 5.03704294266499, 5.31060002128531, 4.75645514093279, 
5.91773998390703, 5.35602652864905, 4.91495218448963, 5.26720770118228, 
4.88570540512482, 4.93370592507765, 4.94560478628556), .Dim = c(10L, 
7L), .Dimnames = list(c("CHEMBL10:O00141", "CHEMBL10:O00311", 
"CHEMBL10:O00444", "CHEMBL10:O14757", "CHEMBL10:O15075", "CHEMBL10:O15530", 
"CHEMBL10:O43293", "CHEMBL10:O43781", "CHEMBL10:O60674", "CHEMBL10:O94806" 
), c("reference", "FP2/SW", "FP3/SW", "FP4/SW", "FP2/GSK", "FP3/GSK", 
"FP4/GSK"))) 

回答

3

我們可以用max.col

m1[,-1][cbind(1:nrow(m1), max.col(-abs(m1[,-1]-m1[,1])))] 
#[1] 5.082673 5.514908 5.063755 5.440714 4.975569 5.060028 5.267208 5.207623 5.082929 5.161291 

編輯:基於@ RHertel的建議