cran / graph4lg

:exclamation: This is a read-only mirror of the CRAN R package repository. graph4lg — Build Graphs for Landscape Genetics Analysis
3 stars 3 forks source link

Error in genind_to_genepop with 2 populations #1

Open THccaa opened 3 years ago

THccaa commented 3 years ago

I tried to convert a genind object to a genpop file to genind_to_genepop and I received this error:

Error in (num_end_pop[i] + 1):num_end_pop[i + 1] : NA/NaN argument

It comes from this loop (line 172:176):

# Then, add the rows of each population, one line with "POP", etc..
  for (i in 1:(length(num_end_pop)-1)){
    data_gpop2 <- rbind(data_gpop2, c("Pop", rep("", n.loci)),
                        data_gpop[(num_end_pop[i]+1):num_end_pop[i+1], ])
  }

With only two populations the length of num_end_pop is 1, which creates this error. This can be avoided with a simple condition in the case of only 2 populations. I changed the lines 172:180 in the function to:

if(length(levels(x$pop)) == 2){
      # Then, add the rows of each population, one line with "POP", etc..
      data_gpop2 <- rbind(data_gpop2, c("Pop", rep("", n.loci)),
                          data_gpop[(num_end_pop+1):nrow(data_gpop), ])
    } else{
      # Then, add the rows of each population, one line with "POP", etc..
      for (i in 1:(length(num_end_pop)-1)){
        data_gpop2 <- rbind(data_gpop2, c("Pop", rep("", n.loci)),
                            data_gpop[(num_end_pop[i]+1):num_end_pop[i+1], ])
        # Add the last population
        data_gpop2 <- rbind(data_gpop2, c("Pop", rep("", n.loci)),
                            data_gpop[(num_end_pop[length(num_end_pop)]+1):nrow(data_gpop), ])
  }

This may not be very elegant, but it worked for me.

gaborcsardi commented 3 years ago

Hi, this is a read only mirror of CRAN. Please see the package authors and possibly URLs in the DESCRIPTION file. Thanks!