找到R中每行最接近第一列中的值的值

时间:2016-05-16 09:39:24

标签: r matrix

我有一个包含7列和10,000行的矩阵。第一列是我的reference列,因此我想浏览每一行,并希望将第2列到第7列的值与该特定行的reference列中的值进行比较,然后选择一行最接近reference列中的值。如果没有很多for / if的话,怎么可能有效地做到这一点?

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.8519813210051, 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")))

1 个答案:

答案 0 :(得分: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的建议