mercredi 4 juillet 2018

Need to get index of only values selected from if statement in R

absolute <- abs(AlignmentBlockLength - TargetLength)

for(i in as.matrix(na.omit(absolute))) {
  if(c(i) <= 20) {
    candidates[length(candidates)+1] <- c(i)
  }
}

This runs so far^

I have two columns stored as data frames "AlignmentBlockLength" and "TargetLength." Each column has 3100 elements, but I am trying to compare the variables then select the differences that are less than or equal to 20.

I am stuck however since I don't know how to specifically get the index of the values that are selected in the if statement from the "AlignmentBlockLength" column. Is there a way to store only the index within a vector or data.frame in R?

Columns after selecting only less than or equal to 20:

AlignmentBlockLength  TargetLength  Absolute
  3300                   3280          20
  2587                   2600          13
  1569                   1539          30

What I want to store in a new column or value: (indexes from AlignmentBlockLength)

index  AlignmentBlockLength  TargetLength  Absolute
  256     3300                   3280          20
  31      2587                   2600          13
  346     1569                   1539          30

Aucun commentaire:

Enregistrer un commentaire