From 138ce99c639c78a609eaabf290a8d83b3815c5b0 Mon Sep 17 00:00:00 2001
From: Clare <chorscroft@users.noreply.github.com>
Date: Tue, 8 Sep 2020 21:31:35 +0100
Subject: [PATCH] Chnage to L_plus_R so that it will return zero rather than NA
 when n<2 in choose(n,2)

---
 R/L_plus_R.R                   | 7 ++-----
 tests/testthat/test-L_plus_R.R | 4 ++--
 2 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/R/L_plus_R.R b/R/L_plus_R.R
index 848b6b2..70316df 100644
--- a/R/L_plus_R.R
+++ b/R/L_plus_R.R
@@ -66,11 +66,8 @@ L_plus_R <- function(pos, ws, X = NULL) {
     ## get L, R and L_plus_R
     noL <- length(pos[pos>=currentPos-ws/2 & pos < currentPos]) ## Number of SNPs to the left of the current SNP
     noR <- length(pos[pos<=currentPos+ws/2 & pos > currentPos]) ## Number of SNPs to the right of the current SNP
-    if(noL < 2 || noR < 2){  #Must be at least 2 to calculate n choose 2
-      outputList$L_plus_R[i]<-NA
-    } else {
-      outputList$L_plus_R[i]<-choose(noL,2)+choose(noR,2)
-    }
+    ## if n < 2 then choose function will return 0
+    outputList$L_plus_R[i]<-choose(noL,2)+choose(noR,2)
   }
   return(outputList)
 }
diff --git a/tests/testthat/test-L_plus_R.R b/tests/testthat/test-L_plus_R.R
index 3680384..2e02d9e 100644
--- a/tests/testthat/test-L_plus_R.R
+++ b/tests/testthat/test-L_plus_R.R
@@ -12,7 +12,7 @@ test_that("L_plus_R calculates L_plus_R statistic correctly", {
   expect_equal(L_plus_R(pos = df$POS, ws  = 3000, X = NULL),
                list(
                  position=c(100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500),
-                 L_plus_R=c(NA,NA,67,58,51,46,43,42,43,46,51,58,67,NA,NA)
+                 L_plus_R=c(91,78,67,58,51,46,43,42,43,46,51,58,67,78,91)
                ))
 })
 
@@ -23,7 +23,7 @@ test_that("L_plus_R calculates L_plus_R statistic correctly with a different win
   expect_equal(L_plus_R(pos = df$POS, ws  = 1100, X = NULL),
                list(
                  position=c(100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500),
-                 L_plus_R=c(NA,NA,11,13,16,20,20,20,20,20,16,13,11,NA,NA)
+                 L_plus_R=c(10,10,11,13,16,20,20,20,20,20,16,13,11,10,10)
                ))
 })
 
-- 
GitLab