#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 [EMAIL PROTECTED] 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@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.