Is there a more efficient way of doing function 'which' in R? The data file is here.

Here are my codes:

&gt; ftable=read.table(file.choose())
&gt; start.time=Sys.time()
&gt; 1-length(which(ftable==1))/sum(ftable)
[1] 0.12
&gt; end.time=Sys.time()
&gt; end.time-start.time
Time difference of 0.004880905 secs


I understand that 0.00488 secs are not a lot. But I have to repeatedly do this calculation over different and larger tables. I am wondering if the function 'which' can be replaced by a more efficient one.

Thanks in advance!

Note: This piece of codes is to calculate the percentage of singletons in ftable. If there is a more efficient way, please let me know. Thank you!
Try:

1-sum(ftable==1L)/sum(ftable)


Test on larger data:

n &lt;- 1000000
set.seed(21)
ftable &lt;- data.frame(replicate(3, sample.int(4, n, replace = T))-1L)

start.time=Sys.time()
1-length(which(ftable==1))/sum(ftable)
end.time=Sys.time()
end.time-start.time
# Time difference of 0.1981359 secs

start.time=Sys.time()
1-sum(ftable==1L)/sum(ftable)
end.time=Sys.time()
end.time-start.time
# Time difference of 0.06704712 secs


bechmarks:

n &lt;- 1000000
set.seed(21)
ftable &lt;- data.frame(replicate(3, sample.int(4, n, replace = T))-1L)

jz &lt;- function() 1-length(which(ftable==1))/sum(ftable)  
minem &lt;- function() 1-sum(ftable==1L)/sum(ftable)

br &lt;- bench::mark(jz(), minem(), iterations = 50)
br[, 1:7]
# A tibble: 2 x 7
#   expression      min     mean   median      max `itr/sec` mem_alloc
#   &lt;chr&gt;      &lt;bch:tm&gt; &lt;bch:tm&gt; &lt;bch:tm&gt; &lt;bch:tm&gt;     &lt;dbl&gt; &lt;bch:byt&gt;
# 1 jz()         51.2ms   53.8ms   52.6ms   66.3ms      18.6    60.1MB
# 2 minem()      37.7ms   39.9ms   38.5ms     67ms      25.1    45.8MB
# only around 36 % faster