mercredi 19 mai 2021

Assigning variable value according to ifelse()

Im trying to assign a value to output from a function where i want to down sample data. The function should iterate over the data set in chunks but in case the iterator k (not shown here) is through with the whole data set the function should return a random chunk.

The problem is when trying to discriminate by checking the indices and assigning a random value the value returned from the ifelse() statement is a single integer.

n = c(1:972) #indices of negative class
chunkSize = 98
start = 1
stop = -97
n = ifelse(stop < 0,
           n[sample( n, chunkSize)],
           n[start:stop]
)

length(n) [1] 1 n [1] 261

The line n = n[sample( n, chunkSize)]works fine without the ifelse()

> length(n)
[1] 98
> n
 [1] 698 129  85 683 563 598 366 876 965 569 900  54 141 734  33  96 736 311 685 202 663 597
[23] 795 625 763 961 624 352 162 753 524 693 229 173 544 493 477 526 367 716 525 732 198 211
[45] 675 413 708 745 669 715 267 639 519 730  18 588 850  99   4 691 560 682 807 566 568 304
[67]  69 798 474 747 684 117 704 258 410 618 381 281 429 399 824 244  31 558 253 135 326 328
[89] 452 228 608  38 216 156 773 446 827 528

I Guess it has to do with the ifelse() but I'm a beginner on R and I've spent way to much time trying to fix this by now.

Can someone point out what's causing this behavior or come up with an alternative solution?

Many thanks!

Aucun commentaire:

Enregistrer un commentaire