I think this gives you what you are looking for:
> P.genotype.sample
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 2 6 2 2 3 6 1 5 5 5
[2,] 2 3 2 8 3 3 5 5 5 8
[3,] 1 3 2 8 8 8 5 5 8 8
[4,] 5 6 3 3 3 8 1 5 5 8
[5,] 1 8 3 8 6 6 5 7 5 8
[6,] 1 1 2 2 1 8 1 6 5 5
[7,] 5 6 2 8 1 7 1 7 5 8
[8,] 6 7 2 3 1 3 5 5 2 5
[9,] 1 2 3 4 1 1 5 5 5 8
[10,] 3 3 3 3 3 7 5 8 2 2>
> # create result matrix
> result <- matrix(0, ncol=5, nrow=8)
>
> # iterate for pairs of columns
> for (i in seq(1, ncol(P.genotype.sample), by=2)){
+ # get the rows that match
+ match.row <- P.genotype.sample[,i] == P.genotype.sample[, i + 1]
+ # get the values from the rows that match
+ same <- P.genotype.sample[match.row, i]
+ # now increment the 'result' matrix
+ for (j in same) result[j, (i + 1) / 2] <- result[j, (i + 1) / 2] + 1
+ }> result
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 1 0 0
[2,] 0 2 0 0 1
[3,] 1 2 1 0 0
[4,] 0 0 0 0 0
[5,] 0 0 0 4 2
[6,] 0 0 1 0 0
[7,] 0 0 0 0 0
[8,] 0 0 1 0 1>
On 10/2/07, Luke Neraas <lukasneraas.r at gmail.com>
wrote:> #Hello,
> #I have a question about obtaining results from a loop I have written.
> #Below is a sample of individual genotypes from a genetic question I am
> working on called "P.genotype.sample ".
>
> P.genotype.sample<-matrix(10,10,10)
> P.genotype.sample[,1]<-c(2,2,1,5,1,1,5,6,1,3)
> P.genotype.sample[,2]<-c(6,3,3,6,8,1,6,7,2,3)
> P.genotype.sample[,3]<-c(2,2,2,3,3,2,2,2,3,3)
> P.genotype.sample[,4]<-c(2,8,8,3,8,2,8,3,4,3)
> P.genotype.sample[,5]<-c(3,3,8,3,6,1,1,1,1,3)
> P.genotype.sample[,6]<-c(6,3,8,8,6,8,7,3,1,7)
> P.genotype.sample[,7]<-c(1,5,5,1,5,1,1,5,5,5)
> P.genotype.sample[,8]<-c(5,5,5,5,7,6,7,5,5,8)
> P.genotype.sample[,9]<-c(5,5,8,5,5,5,5,2,5,2)
> P.genotype.sample[,10]<-c(5,8,8,8,8,5,8,5,8,2)
> P.genotype.sample
>
> # I would like to count the number of times the same number ( 1 through 8)
> occurs in each row
> # and only in pairs of columns; 1:2, 3:4, 5:6, 7:8, & 9:10. below is
the
> loop that i have written
> # to perform this operation and put in the results in a matrix that is 5
> columns wide and 8 rows long (called "result").
> # I can get the operation to run but it gives me the results for the last
> pair of columns and overwrites
> # the results into every column of my result matrix. I am puzzled as to why
> this is occurring.
>
> Loci<-5
> Locus.Columns<- Loci*2
>
> x<-seq(1,Locus.Columns,2)
>
> result<-matrix(8,8,Loci)
>
>
> for (i in 1:Loci){
> for (j in 1:8){
> for (k in x){
> result[j,i] <- sum(P.genotype.sample[,k]==j &
P.genotype.sample[,k+1]==j)
>
> }
> }
> }
>
> result
>
>
> # Below is a long and drawn out way to achieve the result I am looking for
> in the loop above.
>
> true.result<-matrix(8,8,Loci)
>
> for(j in 1:8){
> true.result[j,1] <-sum(P.genotype.sample[,1]==j &
P.genotype.sample[,2]==j)
> }
>
> for(j in 1:8){
> true.result[j,2] <-sum(P.genotype.sample[,3]==j &
P.genotype.sample[,4]==j)
> }
>
> for(j in 1:8){
> true.result[j,3] <-sum(P.genotype.sample[,5]==j &
P.genotype.sample[,6]==j)
> }
>
> for(j in 1:8){
> true.result[j,4] <-sum(P.genotype.sample[,7]==j &
P.genotype.sample[,8]==j)
> }
>
> for(j in 1:8){
> true.result[j,5] <-sum(P.genotype.sample[,9]==j &
P.genotype.sample[,10]==j)
> }
>
> true.result
>
> # Any suggestions or help on how to make this loop work would be greatly
> appreciated.
>
> # Thanks in advance
>
>
> 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
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> 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?