0

I have a logical vector with only one TRUE value in it

> summary(no_in_gos)
   Mode   FALSE    TRUE 
logical    5891       1 

When I use it to subset a dataframe, it returns multiple rows, which I don't understand. I'm only expecting one row, given that there is only one TRUE element in the logical vector:

> gos2[no_in_gos,]
      ensembl_gene_id      go_id
24            YDL245C GO:0005355
5916          YBR015C GO:0000026
11808         YPR182W GO:0046540
17700         YJR090C GO:0005634
23592       YFL034C-B GO:0007163
...
...

I'm really sure that my logical vector has only one TRUE element:

> sum(no_in_gos)
[1] 1
> which(no_in_gos == 'TRUE')
[1] 24

What can be some of the reasons that explain this behavior?

matrs
  • 59
  • 1
  • 6
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Does `length(no_in_gos)` equal `nrow(gos2)`? – MrFlick Oct 21 '19 at 16:32
  • Those dataframes are relatively big, are the result of other previous steps in a pipeline, is not something that i have encountered before nor I can make an example, because I really don't understand why this happens – matrs Oct 21 '19 at 16:35
  • 2
    This looks like vector recycling. Notice that the row numbers are `24+5892*0:4` You are using a vector of length `5892` to subset a much larger table. The shorter vector is being recycled to the length of the longer. Not sure exactly what you are trying to subset given that scenario. – MrFlick Oct 21 '19 at 16:38
  • I see, that seems to be the cause.... I was expecting that is something basic about subsetting in R that I didn't know, I'll check the documentation better. – matrs Oct 21 '19 at 16:42

0 Answers0