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.
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):
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:
This may not be very elegant, but it worked for me.