Home > other >  Interpolate/downscale vector to a given percentage in R
Interpolate/downscale vector to a given percentage in R

Time:01-17

I have a following dummy vector:

vctr <- c(601, 629, 652, 688, 653, 633, 625, 622, 653, 667, 605, 595, 667, 653, 
          602, 597, 659, 656, 706, 710, 718, 711, 725, 682, 656, 676, 698, 680, 
          689, 702, 689, 686, 701, 730, 736, 727, 685, 656, 692, 691, 685, 662, 
          678, 695, 720, 718, 694, 701, 713, 724, 685, 665, 664, 702, 680, 685, 
          709, 711, 681, 678, 715, 722, 737, 726, 716, 714, 727, 725, 717, 719, 
          730, 749, 736, 721, 704, 720, 719, 725, 712, 713, 708, 711, 706, 693, 
          682, 693, 701, 702, 699, 696, 693, 688, 695, 694, 687, 687, 699, 707, 
          688, 680, 688, 698, 700, 703, 704, 693, 693, 700, 708, 704, 704, 701, 
          699, 684, 678, 690, 700, 703, 709, 707, 687, 686, 698, 704, 700, 691, 
          686, 686, 699, 717, 709, 697, 694, 688, 676, 676, 689, 701, 720, 721, 
          708, 702, 702, 707, 700, 691, 693, 701, 691, 682, 681, 681, 690, 710, 
          718, 708, 705, 710, 709, 690, 688, 695, 699, 699, 695, 688, 690, 706, 
          712, 707, 706, 704, 701, 694, 679, 694, 690, 695, 690, 700, 699, 709, 
          709, 696, 694, 698, 694, 693, 693, 701, 695, 698, 702, 707, 703, 702, 
          697, 686, 684, 681, 675, 676, 688, 693, 703, 708, 697, 688, 684, 701, 
          697, 702, 696, 704, 696, 690, 688, 680, 686, 702, 714, 696, 687, 692, 
          697, 690, 690, 688, 701, 693, 687, 668, 669, 675, 684, 693, 693, 697, 
          694, 693, 692, 690, 701, 691, 693, 699, 696, 677, 686, 692, 687, 689, 
          705, 696, 688, 671, 670, 670, 687, 698, 694, 687, 682, 690, 675, 688, 
          703, 705, 682, 673, 679, 684, 695, 689, 691, 662, 662, 664, 688, 677, 
          678, 676, 692, 690, 689, 681, 684, 666, 662, 659, 665, 666, 676, 665, 
          660, 667, 695, 708, 696, 670, 664, 670, 674, 668, 666, 663, 658, 658, 
          658, 665, 672, 684, 686, 691, 683, 680, 679, 684, 679, 677, 677, 679, 
          685, 685, 679, 655, 650, 659, 665, 653, 651, 665, 669, 644, 600, 567, 
          549, 551, 549, 543, 541, 566, 576, 564, 546, 546, 534, 537, 530, 534, 
          535, 538, 549, 554, 529, 543, 623, 644, 601, 562, 553, 521, 510, 517, 
          515, 509, 519, 556, 613, 594, 565, 533, 541, 553, 548, 601, 607, 605, 
          564, 584, 633, 693, 711, 711, 725, 729, 720, 739, 734, 708, 703, 709, 
          717, 728, 726, 728, 720, 721, 700, 703, 714, 733, 727, 718, 746, 764, 
          773, 756, 737, 725, 724, 733, 718, 703, 707, 729, 729, 715, 720, 714, 
          703, 693, 701, 693, 688, 681, 694, 707, 702, 701, 709, 720, 713, 705, 
          694, 683, 690, 696, 693, 701, 717, 724, 706, 700, 702, 712, 709, 700, 
          694, 690, 688, 677, 682, 691, 698, 692, 702, 716, 711, 708, 709, 700, 
          702, 715, 715, 695, 690, 696, 705, 702, 688, 689, 696, 704, 703, 715, 
          710, 706, 699, 704, 704, 702, 701, 700, 696, 692, 691, 699, 711, 714, 
          700, 710, 715, 712, 707, 715, 717, 724, 723, 709)

I would like to downsize (interpolate) it, so it would be shorter, but it will retain its properties (shape, scale etc.). To be more specific (non-native here, sorry) I would like to perform operation similar to rescaling/converting image between various formats/compression levels such as .png, .jpeg etc..

I found an approx() function from stats package. Using this I can resize the vector to, for example, 1/5 of its initial length:

vctr_lin <- stats::approx(vctr, method = "linear", n=100)
vctr_const <- stats::approx(vctr, method = "constant", n=100)

It basically does what I want, but it is slow. I would prefer something faster. Also, if it is possible, I would prefer a solution which would allow to shrink the vector to a given percentage. For instance to obtain an output vector which would be of 80% length of the initial one. It would be helpful for me, as IRL I have a set of vectors, similar to this dummy, but of various length. And I would have them all downscaled to a given percentage.

CodePudding user response:

You could try to sample without replacement. The new data should retain overall characteristics of the original data. Check to make sure the properties you are interested in is retained.

v2 = sample(x = vctr, size = ceiling(0.8 * length(vctr)))

# Compare original and new data
graphics.off()
plot(1:500, sort(vctr), col = "red")
points(seq(1, 500, length.out = length(v2)), sort(v2), col = "blue")

CodePudding user response:

Your idea changes the structure of the data. I plotted both vectors:

Here is my original vctr: https://i.stack.imgur.com/XTjNY.png

And here is your v2: https://i.stack.imgur.com/TBeIB.png

So this unfortunately does not solve my problem.

  •  Tags:  
  • Related