Note that there are some explanatory texts on larger screens.

plurals
  1. POR - function simplification and optimization
    primarykey
    data
    text
    <p>What the function does is finds the <code>primes</code> in <code>list1</code> and converts them to their equivalent <code>a</code> (i.e. 2 would be <code>a2</code> which is 1,1). Then it takes the <code>!NULL</code> in <code>list2</code> and searches if it is present in the converted <code>list1</code> (converted to a list of two of 1,2,3, or 4). Finally it returns a logical list of true and false which will be used later on. Even tough it works it looks really inefficient and long. Looking for any suggestions to improve this. </p> <p>Test data:</p> <pre><code>list1&lt;-c(11,2,29,5,17) offspring&lt;-data.frame(matrix(unlist(list1), nrow=1, byrow=T)) list2&lt;-c(0,11) parent&lt;-data.frame(matrix(unlist(list2), nrow=1, byrow=T)) </code></pre> <p>Prerequisites:</p> <pre><code>primes &lt;- c(2, 3, 5, 7, 11, 13, 17, 19, 23, 29) a2 &lt;- c(1,1) a3 &lt;- c(1,2) a5 &lt;- c(2,2) a7 &lt;- c(1,3) a11 &lt;- c(1,4) a13 &lt;- c(2,3) a17 &lt;- c(2,4) a19 &lt;- c(3,3) a23 &lt;- c(3,4) a29 &lt;- c(4,4) </code></pre> <p>The missing function:</p> <pre><code>missing &lt;- function(offspring, parent){ ofls &lt;- lapply(1:ncol(offspring),FUN = function(i){get(paste0("a", offspring[i]))}) pals &lt;- lapply(1:length(parent[parent&gt;0]),FUN = function(i){get(paste0("a", parent[parent&gt;0]))}) ofls&lt;-data.frame(ofls) pals&lt;-data.frame(pals) set1&lt;- matrix(,nrow=10,ncol=1) set2&lt;- matrix(,nrow=10,ncol=1) for(z in 1:5){ set1[z,]&lt;-match(ofls[1,z],pals[1,], nomatch=0) set1[z+5,]&lt;-match(ofls[2,z],pals[1,], nomatch=0) set2[z,]&lt;-match(ofls[1,z],pals[2,], nomatch=0) set2[z+5,]&lt;-match(ofls[2,z],pals[2,], nomatch=0) } return((set1+set2)[c(1,2,3,4,5)]+(set1+set2)[c(6,7,8,9,10)]==0) } missing(offspring,parent) </code></pre> <p>Result:</p> <pre><code>[1] TRUE TRUE FALSE TRUE FALSE </code></pre> <p>EDIT: I corrected the mistake I found, in doing so the script go simplified a lot.</p> <p>EDIT 2: I am still getting an error in RStudio after running the function and then trying to <code>view()</code> the two data.frames:</p> <pre><code>Error in View : argument "title" is missing, with no default </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload