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
my Goal: i need to
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.
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.
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
98 lines in row A.
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