# [R] (no subject)

jim holtman jholtman at gmail.com
Fri Oct 19 22:31:56 CEST 2007

```You have this sequence of code in your script:

for (a in seq(1,(ncol(X)-3), by=2)){
for (b in seq(3,(ncol(X)-1), by=2)){
for (i in a){
j <- a+1
for (k in b){
l <- b+1

The 'for (i in a){' appears to meaningless since 'a' is just a single
value.  What did you think this is supposed to do?  Same for 'for (k
in b)'.

On 10/19/07, Luke Neraas <lukasneraas.r at gmail.com> wrote:
> # Hello
> # I  have a question regarding pairwise calculations of a matrix using a
> "for-loop."
> # Below I have a matrix "X" with 8 columns. These are Genotypic data so
> Column1 & Column2 is
> # a unit, Column3 & Column4 is a unit, Column5 & Column6 is a unit,  and
> Coulmn7 & 8 is a unit.
> # I have a loop designed to calculate the number of times an individual in
> Column"i" & Column"j"
> # has the same value and the same individual has two values that are the
> same in Column"k" & Column"l" .
> # I have another seires of code that adds a 2 in the poper location of a
> data frame called "result.df".
> # I have written a loop that accomplishes this "pair of columns" pairwise
> comparison, but it also compares
> # some of the "pairs of Columns" to themselves. Is there a way to get around
> this?
>
>
> # creation of the data matrix
> c1<- c(1,4,3,2,4,1,3,2,4,3)
> c2<- c(2,4,3,4,4,3,4,1,3,2)
> c3<- c(1,3,2,4,4,3,4,4,2,2)
> c4<- c(2,3,2,3,1,3,2,4,4,3)
> c5<- c(1,2,1,1,2,2,2,3,2,1)
> c6<- c(3,2,4,3,1,1,2,3,3,4)
> c7<- c(1,2,1,2,3,2,3,2,1,2)
> c8<- c(1,2,2,3,2,3,3,4,1,2)
>
> X<-cbind(c1,c2,c3,c4,c5,c6,c7,c8)
>
> X
>
> ## Creation of the result dataframe
> result<- matrix(0,16,2)
> result.df<-data.frame(result)
> result.df[,1] <- c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4)
> result.df[,2] <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
> names(result.df)[1]<-"L(A)a(i)"
> names(result.df)[2]<-"L(B)a(j)"
>
> result.df
>
>
>
> ### The loop written to find Double Homozygotes
>
>
> for (a in seq(1,(ncol(X)-3), by=2)){
> for (b in seq(3,(ncol(X)-1), by=2)){
> for (i in a){
>    j <- a+1
> for (k in b){
>    l <- b+1
>
>    match.rows <- ((X [,i] == X [, j] ) &   ( X [,k] == X [, l]))
>
>    double_homo_i <- X [match.rows, i]
>    double_homo_k <- X [match.rows, k]
>
>    double_homo<- cbind( double_homo_i, double_homo_k)
>    double_homo.df<-data.frame(double_homo,Counts=2)
>       names(double_homo.df)[1]<-"L(A)a(i)"
>       names(double_homo.df)[2]<- "L(B)a(j)"
>
>
> # Below takes each round throught he loop and puts in the result.dfdataframe.
>
> count<-double_homo.df
>
> almost.df<-aggregate(count\$Counts, list(count[,1],count[,2]),
> FUN=sum)
>
> temp<-order(almost.df\$Group.1)
> final.df<-almost.df[temp,]
> names(final.df)[1]<-"L(A)a(i)"
> names(final.df)[2]<-"L(B)a(j)"
>
> result.df<-merge(result.df,final.df,by=c("L(A)a(i)","L(B)a(j)"), all.x=T)
>
>             }
>             }
>             }
>             }
>
>
> # here is the result I get
>
> result.df
>
>
>
> #     L(A)a(i) L(B)a(j) C1C2~C3C4 C1C2~C5C6 C1C2~C7C8 C3C4~C3C4 C3C4~C5C6
> C3C4~C7C8 C5C6~C3C4 C5C6~C5C6 C5C6~C7C8
> #  1         1        1       NA        NA         NA        NA
> NA        NA        NA        NA        NA
> #  2         1        2       NA        NA         NA        NA
> NA        NA        NA        NA        NA
> #  3         1        3       NA        NA         NA        NA
> NA        NA        NA        NA        NA
> #  4         1        4       NA        NA         NA        NA
> NA        NA        NA        NA        NA
> #  5         2        1       NA        NA         NA        NA
> NA        NA        NA        NA        NA
> #  6         2        2       NA        NA         NA         2
> NA        NA        NA         4         2
> #  7         2        3       NA        NA         NA        NA
> NA        NA         2        NA         2
> #  8         2        4       NA        NA         NA        NA
> NA        NA        NA        NA        NA
> #  9         3        1       NA        NA         NA        NA
> NA        NA        NA        NA        NA
> #  10        3        2        2        NA         NA        NA
> 2         2        NA        NA        NA
> #  11        3        3       NA        NA         NA         4
> NA        NA        NA         2        NA
> #  12        3        4       NA        NA         NA        NA
> NA        NA         2        NA        NA
> #  13        4        1       NA        NA         NA        NA
> NA        NA        NA        NA        NA
> #  14        4        2       NA         2          2        NA
> NA        NA        NA        NA        NA
> #  15        4        3        2        NA         NA        NA
> 2        NA        NA        NA        NA
> #  16        4        4       NA        NA         NA         2
> NA        NA        NA        NA        NA
>
>
>
>
>
> # Here is the Result I am looking for.
>
>     L(A)a(i) L(B)a(j) C1C2~C3C4 C1C2~C5C6 C1C2~C7C8 C3C4~C5C6 C3C4~C7C8
> C5C6~C7C8
> # 1         1        1        NA        NA        NA        NA
> NA        NA
> # 2         1        2        NA        NA        NA        NA
> NA        NA
> # 3         1        3        NA        NA        NA        NA
> NA        NA
> # 4         1        4        NA        NA        NA        NA
> NA        NA
> # 5         2        1        NA        NA        NA        NA
> NA        NA
> # 6         2        2        NA        NA        NA        NA
> NA         2
> # 7         2        3        NA        NA        NA        NA
> NA         2
> # 8         2        4        NA        NA        NA        NA
> NA        NA
> # 9         3        1        NA        NA        NA        NA
> NA        NA
> # 10        3        2         2        NA        NA         2
> 2        NA
> # 11        3        3        NA        NA        NA        NA
> NA        NA
> # 12        3        4        NA        NA        NA        NA
> NA        NA
> # 13        4        1        NA        NA        NA        NA
> NA        NA
> # 14        4        2        NA         2         2        NA
> NA        NA
> # 15        4        3         2        NA        NA         2
> NA        NA
> # 16        4        4        NA        NA        NA        NA
> NA        NA
>
>
> # Any help or ideas would be greatly appreciated
>
>
> # Luke Neraas
>
> # lukasneraas.r at gmail.com
>
> # University of Alaska Fairbanks
> # School of Fisheries and Ocean Sciences
> # 11120 Glacier Highway
> # UAF Fisheries Division
> # Juneau, AK 99801
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

```