Considering groups (gp
) of n results (ngp
), how to select/subset given numbers of results (nesgp
) that are spaced as evenly as possible between the minimum and maximum (both necessarily included) in a new column selec
?
Edit: Ideally, unselected results should appear as NA
in the new selec
column, not duplicated.
> print(dat, n=56)
# A tibble: 56 x 4
gp result ngp nesgp
<chr> <dbl> <dbl> <dbl>
1 CA 1.64 24 15
2 CA 1.69 24 15
3 CA 1.71 24 15
4 CA 1.74 24 15
5 CA 1.78 24 15
6 CA 1.82 24 15
7 CA 1.86 24 15
8 CA 1.9 24 15
9 CA 1.94 24 15
10 CA 1.98 24 15
11 CA 2.6 24 15
12 CA 2.65 24 15
13 CA 2.71 24 15
14 CA 2.76 24 15
15 CA 2.83 24 15
16 CA 2.89 24 15
17 CA 2.94 24 15
18 CA 3 24 15
19 CA 3.22 24 15
20 CA 3.42 24 15
21 CA 3.47 24 15
22 CA 3.68 24 15
23 CA 3.85 24 15
24 CA 4.38 24 15
25 ASAT 9 20 12
26 ASAT 11 20 12
27 ASAT 51 20 12
28 ASAT 61 20 12
29 ASAT 69 20 12
30 ASAT 78 20 12
31 ASAT 89 20 12
32 ASAT 102 20 12
33 ASAT 111 20 12
34 ASAT 120 20 12
35 ASAT 146 20 12
36 ASAT 163 20 12
37 ASAT 189 20 12
38 ASAT 208 20 12
39 ASAT 218 20 12
40 ASAT 304 20 12
41 ASAT 332 20 12
42 ASAT 345 20 12
43 ASAT 362 20 12
44 ASAT 402 20 12
45 ORO 0.56 12 8
46 ORO 0.7 12 8
47 ORO 0.77 12 8
48 ORO 0.78 12 8
49 ORO 0.82 12 8
50 ORO 0.82 12 8
51 ORO 0.92 12 8
52 ORO 0.94 12 8
53 ORO 1.16 12 8
54 ORO 1.46 12 8
55 ORO 1.54 12 8
56 ORO 1.77 12 8
Data
dat <-
structure(list(gp = c("CA", "CA", "CA", "CA", "CA", "CA", "CA",
"CA", "CA", "CA", "CA", "CA", "CA", "CA", "CA", "CA", "CA", "CA",
"CA", "CA", "CA", "CA", "CA", "CA", "ASAT", "ASAT", "ASAT", "ASAT",
"ASAT", "ASAT", "ASAT", "ASAT", "ASAT", "ASAT", "ASAT", "ASAT",
"ASAT", "ASAT", "ASAT", "ASAT", "ASAT", "ASAT", "ASAT", "ASAT",
"ORO", "ORO", "ORO", "ORO", "ORO", "ORO", "ORO", "ORO", "ORO",
"ORO", "ORO", "ORO"), result = c(1.64, 1.69, 1.71, 1.74, 1.78,
1.82, 1.86, 1.9, 1.94, 1.98, 2.6, 2.65, 2.71, 2.76, 2.83, 2.89,
2.94, 3, 3.22, 3.42, 3.47, 3.68, 3.85, 4.38, 9, 11, 51, 61, 69,
78, 89, 102, 111, 120, 146, 163, 189, 208, 218, 304, 332, 345,
362, 402, 0.56, 0.7, 0.77, 0.78, 0.82, 0.82, 0.92, 0.94, 1.16,
1.46, 1.54, 1.77), ngp = c(24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12),
nesgp = c(15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -56L))
Thanks for help.
I'm not sure what you mean by "spaced as evenly as possible" but I wrote an example that uses sampling of # of points to minimize the spread between their deltas that could be a good starting point for you:
There are not as many points selected in this case as what you seem to be looking for.
Updated as per comment to show another approach.
If you would like to first determine an ideal distribution based on an evenly spread number of points, you'll just have to come up with that arbitrary number
num_intervals <- length(x)-1
Here are the functions that make the coding a little easier
And here is how to go about performing the algo
We can also visualize the results by doing the following
You'll notice this approach doesn't give you quite the same visual even spread as the previous approach.