0

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?

pogibas
  • 27,303
  • 19
  • 84
  • 117
  • 2
    Can you post `head(rad)`. My guess is that you might need: `rad[, X]` instead of `rad[X, ]`. – pogibas Aug 21 '17 at 20:36
  • Its easier to help if you share data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we can copy/paste the code to test it. – MrFlick Aug 21 '17 at 20:40

0 Answers0