找到值在范围内的位置

时间:2015-08-28 06:50:16

标签: r

我有一个数据框

"X1"    "X2"    "X3"    "X4"    "X5"    "X6"    "X7"    "X8"    "X9"    "X10"   "X11"   "X12"   "X13"   "X14"   "X15"   "X16"   "X17"   "X18"   "X19"   "X20"   "X21"   "X22"   "X23"   "X24"
    13062   145 9   2   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    586717  66068   18713   7105    3188    1506    750 460 251 141 81  41  39  14  12  6   1   0   1   0   0   0   0   0
    55893   1371    155 43  9   3   3   2   0   2   0   1   0   1   0   1   0   0   0   0   0   0   0   0
    63123   2573    266 35  10  6   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    14258   63  4   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    751302  43884   6109    1280    368 123 36  18  6   0   2   1   0   0   0   0   0   0   0   0   0   0   0   0
    45018   1017    89  10  6   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    182932  6581    941 238 73  43  20  9   6   8   2   1   1   0   0   1   0   1   0   0   0   0   0   0
    23741   356 4   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    4391    15  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    20201   546 75  17  1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    48193   943 50  9   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    11284   89  1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    4819    48  2   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

我找到了每一行的意思

df$mean=apply(df,1,mean)

"X1"    "X2"    "X3"    "X4"    "X5"    "X6"    "X7"    "X8"    "X9"    "X10"   "X11"   "X12"   "X13"   "X14"   "X15"   "X16"   "X17"   "X18"   "X19"   "X20"   "X21"   "X22"   "X23"   "X24"   "mean"
    13062   145 9   2   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   550.75
    586717  66068   18713   7105    3188    1506    750 460 251 141 81  41  39  14  12  6   1   0   1   0   0   0   0   0   28545.5833333333
    55893   1371    155 43  9   3   3   2   0   2   0   1   0   1   0   1   0   0   0   0   0   0   0   0   2395.16666666667
    63123   2573    266 35  10  6   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   2750.54166666667
    14258   63  4   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   596.875
    751302  43884   6109    1280    368 123 36  18  6   0   2   1   0   0   0   0   0   0   0   0   0   0   0   0   33463.7083333333
    45018   1017    89  10  6   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1922.5
    182932  6581    941 238 73  43  20  9   6   8   2   1   1   0   0   1   0   1   0   0   0   0   0   0   7952.375
    23741   356 4   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1004.20833333333
    4391    15  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   183.583333333333
    20201   546 75  17  1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   868.333333333333
    48193   943 50  9   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   2049.79166666667
    11284   89  1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   473.916666666667
    4819    48  2   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   202.875

我想在第1列到第24列的范围内找出平均值所在的位置。

例如:

如果我的x值为1,2,3,4,y为10,20,30,40。如果我在x中找到对应于15的值,我会得1.5。这里我想找出平均值所在的位置,它可以给我一个浮点数,指定它位于列,例如1.5。

更新:

样本数据

X1  X2  X3  X4  X5  X6  X7  X8  X9  X10 mean
10  20  30  40  50  60  70  80  90  100 55

这意味着5.5。如何计算上表中类似的东西。

可以假设每行中的值都已排序。

1 个答案:

答案 0 :(得分:0)

df<-read.table(text="X1    X2   X3    X4    X5  X6    X7    X8    X9    X10   X11   X12   X13   X14   X15   X16   X17   X18   X19   X20   X21   X22   X23   X24
        13062   145 9   2   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
       586717  66068   18713   7105    3188    1506    750 460 251 141 81  41  39  14  12  6   1   0   1   0   0   0   0   0
       55893   1371    155 43  9   3   3   2   0   2   0   1   0   1   0   1   0   0   0   0   0   0   0   0
       63123   2573    266 35  10  6   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
       14258   63  4   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
       751302  43884   6109    1280    368 123 36  18  6   0   2   1   0   0   0   0   0   0   0   0   0   0   0   0
       45018   1017    89  10  6   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
       182932  6581    941 238 73  43  20  9   6   8   2   1   1   0   0   1   0   1   0   0   0   0   0   0
       23741   356 4   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
       4391    15  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
       20201   546 75  17  1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
       48193   943 50  9   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
       11284   89  1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
       4819    48  2   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0",header=T)

df$mean<-rowMeans(df)

n<-ncol(x)
apply(df,1,function(x) (max(which(x[n]<x[-n]))+min(which(x[n]>x[-n])))/2)

[1] 1.5 2.5 1.5 1.5 1.5 2.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5