I'm using Sunder in R to look at the relationship between genomic variation and the environment. I've run this code before and it worked! Then, when I went to run the analysis for a second time, the code broke. Now, I'm stuck because I can't recreate the input file. Sunder requires an array containing allele counts data for each locus included in the analysis. This should be an R array with dimensions (n,l,a) n: number of geographical locations, l: number of loci, a: max number of alleles. This is what I've done previously, and where I'm currently stuck:
First, I made loaded my genotype matrix in 012 format (my matrix is called "rad"). Then, I made a data frame with two columns: the first column is a list of individuals (rn$Sample), and the second is a list of geographic locations for each individual (rn$Pop):
> class(rn)
[1] "data.frame"
> rn[1:5,]
Sample Pop
1 1523-75227 ID1
2 1523-92422 AK1
3 1523-92430 AK1
4 1523-92433 AK1
5 1523-92439 AK1
Then, I made a list of all possible geographic locations, or populations:
> pops <- sort(unique(rn$Pop))
> class(pops)
[1] "factor"
> pops
[1] AK1 FL1 FL2 ID1 OR PA SK1 SK2 TX1 VA
Levels: AK1 FL1 FL2 ID1 OR PA SK1 SK2 TX1 VA
Now, I want to break up the matrix into a list of matrices for each unique population value:
> mat_list <- lapply(pops, function(x) {
+ rad[rn$Sample[rn$Pop == x], ]
+ })
Error in rad[rn$Sample[rn$Pop == x], ] : subscript out of bounds
This code worked once before... now there is an issue with my function. The traceback suggests R is having a hard time recognizing the first element in my matrix:
Error in rad[rn$sample[rn$pop == x], ] : subscript out of bounds
2.
FUN(X[[i]], ...)
1.
lapply(pops, function(x) { rad[rn$sample[rn$pop == x], ] })
Any suggestions?