I want to write/apply a function that repeats the LD2
function from the pegas
package on a dataset (jaguar), but changes the value of the 'locus' argument each time. The 'locus' argument accepts a vector of length two (e.g. c(1,2), c(2,3), c(77,78), etc.).
For example, I want the code to run loci.pairs[[1]]
, then loci.pairs[[2]]
, loci.pairs[[3]], etc. and output the results as a list.
I have tried doing this with lapply
, for loops and while loops but have run into errors (see below):
Load libraries and data
library(adegenet)
library(pegas)
data("jaguar")
Create a list of pairs of loci
loci.pairs = combn(seq(1,ncol(jaguar)-1), 2, simplify = FALSE)
loci.pairs[[1]]
Compare each pair of loci from loci.pairs list
LD2(jaguar, locus=c(1,2), details=FALSE)
LD2(jaguar, locus=loci.pairs[[1]], details=FALSE)
LD2(jaguar, locus=loci.pairs[[2]], details=FALSE)
LD2(jaguar, locus=loci.pairs[[3]], details=FALSE)
LD2(jaguar, locus=loci.pairs[[4]], details=FALSE)
LD2(jaguar, locus=loci.pairs[[78]], details=FALSE)
lapply (error)
lapply(jaguar, function(x) LD2(jaguar, locus=loci.pairs[[x]], details=FALSE))
Error in loci.pairs[[x]] : recursive indexing failed at level 2