text: user14041: I want to convert a genetic matrix (rows are samples, columns are alleles) into a STRUCTURE file (output .str) within R. Is there a preferred way to achieve this? I've found genind_to_structure in the graph4lg R package, but I wanted to see if there are other approaches.
  An example of what I mean by a "genetic matrix":
  # Build a matrix of alleles
  temp <- lapply(1:30, function(i) sample(1:9, 4, replace = TRUE))
  temp <- sapply(temp, paste, collapse="")
  temp <- matrix(temp, nrow = 10, dimnames = list(paste("ind",1:10), paste("loc",1:3)))
  temp
  
  # Convert matrix to genind format (for adegenet package)
  test <- df2genind(temp, ploidy = 4, sep = "")
  test
  
  # How to convert to STRUCTURE?
  
  If anyone has input, please chime in! Thanks
  
  user14041: I ultimately found this function, which I used for the purpose of converting a genind to a STRUCTURE file. The graph4lg genind_2_structure command would regularly error on me (present working directory couldn't be passed as the output argument).
  
original_text: 14041: <p>I want to convert a genetic matrix (rows are samples, columns are alleles) into a STRUCTURE file (output .str) within R. Is there a preferred way to achieve this? I've found <a href="https://rdrr.io/cran/graph4lg/man/genind_to_structure.html" rel="nofollow noreferrer">genind_to_structure</a> in the <a href="https://rdrr.io/cran/graph4lg/" rel="nofollow noreferrer">graph4lg</a> R package, but I wanted to see if there are other approaches.</p>
  <p>An example of what I mean by a &quot;genetic matrix&quot;:</p>
  <pre><code># Build a matrix of alleles
  temp &lt;- lapply(1:30, function(i) sample(1:9, 4, replace = TRUE))
  temp &lt;- sapply(temp, paste, collapse=&quot;&quot;)
  temp &lt;- matrix(temp, nrow = 10, dimnames = list(paste(&quot;ind&quot;,1:10), paste(&quot;loc&quot;,1:3)))
  temp
  
  # Convert matrix to genind format (for adegenet package)
  test &lt;- df2genind(temp, ploidy = 4, sep = &quot;&quot;)
  test
  
  # How to convert to STRUCTURE?
  </code></pre>
  <p>If anyone has input, please chime in! Thanks</p>
   14041: <p>I ultimately found <a href="https://github.com/lvclark/R_genetics_conv/blob/master/genind2structure.R" rel="nofollow noreferrer">this function</a>, which I used for the purpose of converting a genind to a STRUCTURE file. The graph4lg <a href="https://rdrr.io/cran/graph4lg/man/genind_to_structure.html" rel="nofollow noreferrer">genind_2_structure</a> command would regularly error on me (present working directory couldn't be passed as the <code>output</code> argument).</p>
  