Note that there are some explanatory texts on larger screens.

plurals
  1. POFilling a matrix with a for loop
    primarykey
    data
    text
    <p>I'm using a function from the genetics package called <code>LD()</code>. To simplify what it does, it essentially takes a list of genotypes (A/A, A/C, G/A, etc.) and creates a list of values (D, D', r, etc.). It looks something like this:</p> <pre><code>a=LD(genotype1,genotype2) </code></pre> <p>with the results looking like:</p> <pre><code>Pairwise LD ----------- D D' Corr Estimates: 0.1419402 0.8110866 0.6029553 X^2 P-value N LD Test: 10.90665 0.0009581958 15 </code></pre> <p>I only need values from Corr, so I'd call upon it with <code>a$r</code>.</p> <p>I have 2 dataframes and I want to use that function on their cartesian product:</p> <p><code>df1</code> and <code>df2</code> are the 2 dataframes, with each column (col) represents a list of genotypes. I'm thinking of using a for loop to fill out a matrix:</p> <pre><code>df1=data.frame(c("A/A","C/C","A/A"),c("G/G","T/T","T/T")) df2=data.frame(c("A/T","C/T","C/C"),c("A/A","A/T","G/G")) q=1 # acts as a counter n=length(df1$col1) # All lists are the same length k=length(df2$col2) # These are to set the dimensions of the matrix r=n*k m=matrix(data=NA, nrow=r, ncol=3, byrow=TRUE, dimnames=list(NULL, c("c14","c19","Link"))) for(i in (1:n)) { for(j in (1:k)) { geno1=genotype(df2)[j] #genotype is a function that must be applied to the geno2=genotype(df1)[i] #lists before the LD() function can be used d=LD(geno1,geno2) m=d$r #I only need the values from this section of the output ld[q,]=c(names(df1),names(df2),m) #This is supposed to fill out the matrix #I'm also not sure of how to do that part q=q+1 #this is so that the above line fills in the next row with each iteration } } </code></pre> <p>When I run this, I get an error:</p> <pre><code>Error in dim(a1) &lt;- a1.d : dims [product "some number"] do not match the length of object ["another number"] </code></pre> <p>I'm expecting a 3 column and many rowed matrix with the first column being the name of the first genotype(column names of df1), the second column being the name of the second genotype (column names of df2), and the third column with the values obtained from the <code>LD()</code> function</p> <p>Any advice? Thanks!</p> <p>UPDATE ANSWER: I managed to get it:</p> <pre><code>q=1 # acts as a counter n=length(t1$rs.) k=length(t2$rs.) r=n*k ld=matrix(data=NA, nrow=r, ncol=3, byrow=TRUE, dimnames=list(NULL, c("c14","c19","Link"))) for(i in (1:n)) { for(j in (1:k)) { deq=LD(genotype(g1[,i]),genotype(g2[,j])) m=deq$r ld[q,]=c(i,j,m) q=q+1 } } </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.
 

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