在r中,如何根据其他向量评估两个向量 [英] in r, how to assess the two vectors based on other vectors

查看:106
本文介绍了在r中,如何根据其他向量评估两个向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对r来说还比较陌生,因此不胜感激

i am relatively new to r, so any help is appreciated

样本数据集,还附加了数据集的图像. 图像是示例数据集

sample dataset, also attached an image of the dataset. image is of sample dataset

a           B           C           D
12.97221,   64.78909    1           2
69.64817,   321.90037   2           28
318.87946,  259.29946   3           5
326.17622,  94.7089     9           8
137.54006,  325.34917   5           88
258.06002,  94.77531    6           63
258.92824,  322.20164   7           64
98.57514,   12.96828    8           34
98.46303,   139.27264   9           21
317.22764,  261.25563   10          97

我的目标:我需要

1) look at value in column A
2) find the nearest/closest number in column B
3) test to see if the value in column B has already been selected
4) if the value in column B has already been selected, then ignore and choose the next closest value.
5) once a new, non-duplicated, value is chosen from column B, then
6) Test to see if the value in column C that is on the same row as the value of interest in column A is not the same as the value in column D on the same row as the nearest chosen value in column B
7) if the values in column C and D are NOT the same, then 
8) return the value from column B into a new column
9) if the value in column C and D are the same, then repeat steps 4-7 until a) a new, non-duplicated value is chosen, and b) the value in C and D are not equal. 

这是我到目前为止的代码,该代码解决了找到不替换"最近的数字的问题,但没有解决在选择B列中的值之前考虑C和D列中相似值的问题;由"Chase"从此处开发:

here is the code i have so far, this code solves the problem of finding the nearest number "without replacement" but does not tackle the issue of similar values in columns C and D being considered before the value in column B is chosen; developed by "Chase" from here: How to get the closest element in a vector for every element in another vector without duplicates?

foo <- function(a,b) {

  out <- cbind(a, bval = NA)

  for (i in seq_along(a)) {
    #which value of B is closest?
    whichB <- which.min(abs(b - a[i]))
    #Assign that value to the bval column
    out[i, "bval"] <- b[whichB]
    #Remove that value of B from being chosen again
    b <- b[-whichB]
  }

  return(out)

希望这(下)是对我的问题的更好描述和示例.

hopefully this (below) is a better description and example of my problem.

请参阅调整后的表格,以更好地显示我的问题.查看A列中的12.97221的值,然后评估B列并选择12.96828的值.然后评估C列中与12.97221相对应的值,即1;然后查看D列中对应于12.96828的值(d = 34中的值).由于尚未选择B列中的12.96828的值,并且C和D列中的值不匹配,因此我希望它在E列中返回12.96828.接下来,它将查看69.64817的A列中的第二个值并进行比较将其设置为B列中的值,则应选择64.78909,然后评估是否已选择它.然后评估C列(2)中与B列中的值相对应的值,并评估D列(2)中与C列中的选定值相对应的值.尽管这是第一次选择64.78909,但是C列和D是相同的,因此我需要从94.7089的B列中选择下一个最接近的值,然后评估是否已选择它;它没有.然后评估C列中与A列中的值相对应的值(C = 2中的值),并评估D列中99.4089对应的值(D中的值34中的值)并进行比较.由于尚未选择94.7089的值,并且C列(C = 2的值)和D列(D = 34的值)中的值不相同,因此将94.7089返回E列.

see adjusted table to better show my problem. look at value in column A of 12.97221, then evaluate column B and choose the value of 12.96828. then evaluate the values in column C that correspond to 12.97221, which is 1; then look at the value in column D (value in d=34) that corresponds to 12.96828. Since the value in column B of 12.96828 has not been selected and the values in column C and D do not match, then I would expect it to return 12.96828 in column E. Next it would look at 2nd value in column A of 69.64817 and compare it to the values in column B, it should select 64.78909, then evaluate if it has been chosen. then evaluate value in column C (2) that corresponds to value in column B and evaluate value in column D (2) that corresponds to chosen value in column C. although this is the first time 64.78909 is selected, the values in column C and D are the same, and therefore i need to pick the next closest value from column B of 94.7089, then evaluate whether it has been chosen; it has not. then evaluate the value in column C that corresponds to the value in column A (value in C = 2) and evaluate the value in column D that corresponds to 94.7089 (value in D of 34) and compare them. Since the value of 94.7089 was not chosen yet and the values in columns C (value in C = 2) and D (value in D = 34) are not the same, return 94.7089 into column E.

再次感谢您,希望我能充分描述我的问题

again, thanks in advance and hopefully i described my problem sufficiently

A行中的98行.

     a         b  c  d
1   12.97221 297.91173  1  1
2   69.64817 298.19087  2  2
3  318.87946 169.03864  3  3
4  326.17622 169.32014  4  4
5  137.54006 336.65953  5  5
6  258.06002  94.70890  6  6
7  258.92824  94.77531  7  7
8   98.57514 290.19832  8  8
9   98.46303 290.40790  9  9
10 317.22764 154.38380 10 10
11 316.64421 148.78655 11 11
12 310.73702 153.32877 12 12
13 237.32708 107.83971 13 13
14 250.65386 108.05706 14 14
15 337.09543 180.63118 15 15
16 337.03365 181.02949 16 16
17 301.22772 185.20628 17 17
18 332.93530 185.97922 18 18
19 340.84127 220.40438 19 19
20 357.42706 220.83922 20 20
21 244.89806  83.18630 21 21
22 244.84391  83.28693 22 22
23  97.16921 338.39649 23 23
24 114.62798 338.43398 24 24
25 178.90640  53.22144 25 25
26 175.59257  57.77149 26 26
27 173.32116  60.62938 27 27
28 172.20906  61.93639 28 28
29 246.51226 150.04782 29 29
30 258.00836 150.65750 30 30
31 259.85790 156.03397 31 31
32 326.10208 230.30117 32 32
33 324.96532 230.59314 33 33
34 319.40851 233.05470 34 34
35 146.11989  10.86714 35 35
36 144.63489  12.96828 36 36
37 139.89335  18.90677 37 37
38 119.96566  18.75278 38 38
39 109.18017  28.03931 39 39
40 108.24683  28.87934 40 40
41 302.29211 230.30386 41 41
42 297.28305 233.96142 42 42
43 244.72843  77.53609 43 43
44 244.55468  77.62372 44 44
45 243.47944  78.07812 45 45
46 181.89548  55.90604 46 46
47 180.80139  55.99444 47 47
48 150.37128  59.83512 48 48
49  51.28074 279.08373 49 49
50  50.95031 279.21971 50 50
51  50.57658 279.37713 51 51
52  48.12937 281.07891 52 52
53 154.16485  22.38683 53 53
54 153.48482  22.52214 54 54
55 145.03992  27.13075 55 55
56 108.21414  31.28673 56 56
57 270.96258 182.05611 57 57
58 269.78887 149.38115 58 58
59 256.37371 154.75579 59 59
60 153.74159  25.74645 60 60
61 151.10381  21.27617 61 61
62  97.67447  25.97402 62 62
63  60.73636 259.29946 63 63
64  11.86492 261.25563 64 64
65 287.19987 262.01448 65 65
66 312.08016 234.55050 66 66
67 315.96324 234.79214 67 67
68 323.03643 235.31352 68 68
69  32.71810 333.35849 69 69
70  59.63687 337.21593 70 70
71 276.34373 115.55930 71 71
72 276.31857 115.67837 72 72
73 275.19374 119.76535 73 73
74  97.94697 288.88226 74 74
75  97.60657 289.19108 75 75
76  97.53337 289.26658 76 76
77 173.02153  84.88042 77 77
78 171.27572  86.35787 78 78
79 169.44530  87.38803 79 79
80  87.67228 297.48545 80 80
81  87.54748 297.88451 81 81
82  86.59445 301.10765 82 82
83 332.49688 185.82157 83 83
84 331.19924 186.74459 84 84
85 222.30368  63.98160 85 85
86 221.44599  64.24739 86 86
87 219.66419  64.78909 87 87
88 229.48482 139.27264 88 88
89 228.76817 109.94767 89 89
90 214.77135 105.61337 90 90
91 208.44254 107.75702 91 91
92 224.10799  84.52048 92 92
93 222.94849  87.27893 93 93
94 222.54903  88.00606 94 94
95 222.13538  88.80756 95 95
96 110.52286 321.90037 96 96
97 109.56354 322.20164 97 97
98  75.80737 325.34917 98 98

推荐答案

所以这是您的答案; 解释嵌入答案本身 (我已从您的数据集中删除了逗号)

So here is your answer; The explanation is embedded in the answer itself (I have removed the commas from your dataset)

setwd("~/Desktop/")
df <- read.table("trial.txt",header=T,sep="\t")
names(df) <- c("a","B","C","D")
df_backup <- df
df$newcol <- NA

used <- c()
for (i in seq(1,length(df$a),1)){
  print("######## Separator ########")
  print(paste("searching right match that fits criteria for ",df$a[i],"in column 'a'",sep=""))
  valueA <- df[i,1]
  orderx <- order(abs(df$B-valueA))

  index=1
  while (is.na(df$newcol[i])) {
    j=orderx[index]
    if (df$B[j] %in% used){
      print(paste("passing ",df$B[j], "as it has already been used",sep=""))
      index=index+1
      next
    } else {
      indexB <- j
      valueB <- df$B[indexB]
      print(paste("trying ",valueB,sep=""))

      if (df$C[i] != df$D[indexB]) {
        df$newcol[i] <- df$B[indexB]
        print(paste("using ",valueB,sep=""))
        used <- c(used,df$B[indexB])
      } else {
        df$newcol[i] <- NA
        print(paste("cant use ",valueB,"as the column C (related to index in A) and D (related to index in B) values are matching",sep=""))
      }

      index=index+1
    }
  }
}

输出看起来像这样

[1] "######## Separator ########"
[1] "searching right match that fits criteria for 12.97221in column 'a'"
[1] "trying 12.96828"
[1] "using 12.96828"
[1] "######## Separator ########"
[1] "searching right match that fits criteria for 69.64817in column 'a'"
[1] "trying 64.78909"
[1] "cant use 64.78909as the column C (related to index in A) and D (related to index in B) values are matching"
[1] "trying 94.7089"
[1] "using 94.7089"
[1] "######## Separator ########"
[1] "searching right match that fits criteria for 318.87946in column 'a'"
[1] "trying 321.90037"
[1] "using 321.90037"
[1] "######## Separator ########"
[1] "searching right match that fits criteria for 326.17622in column 'a'"
[1] "trying 325.34917"
[1] "using 325.34917"
[1] "######## Separator ########"
[1] "searching right match that fits criteria for 137.54006in column 'a'"
[1] "trying 139.27264"
[1] "using 139.27264"
[1] "######## Separator ########"
[1] "searching right match that fits criteria for 258.06002in column 'a'"
[1] "trying 259.29946"
[1] "using 259.29946"
[1] "######## Separator ########"
[1] "searching right match that fits criteria for 258.92824in column 'a'"
[1] "passing 259.29946as it has already been used"
[1] "trying 261.25563"
[1] "using 261.25563"
[1] "######## Separator ########"
[1] "searching right match that fits criteria for 98.57514in column 'a'"
[1] "trying 94.77531"
[1] "using 94.77531"
[1] "######## Separator ########"
[1] "searching right match that fits criteria for 98.46303in column 'a'"
[1] "passing 94.77531as it has already been used"
[1] "passing 94.7089as it has already been used"
[1] "trying 64.78909"
[1] "using 64.78909"
[1] "######## Separator ########"
[1] "searching right match that fits criteria for 317.22764in column 'a'"
[1] "passing 321.90037as it has already been used"
[1] "trying 322.20164"
[1] "using 322.20164"

最终表如下:

1   2.97221 64.78909    1   2   12.96828
2   69.64817    321.90037   2   28  94.7089
3   318.87946   259.29946   3   5   321.90037
4   326.17622   94.7089 9   8   325.34917
5   137.54006   325.34917   5   88  139.27264
6   258.06002   94.77531    6   63  259.29946
7   258.92824   322.20164   7   64  261.25563
8   98.57514    12.96828    8   34  94.77531
9   98.46303    139.27264   9   21  64.78909
10  317.22764   261.25563   10  97  322.20164

这篇关于在r中,如何根据其他向量评估两个向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆