Luke Neraas
2007-Sep-26 00:29 UTC
[R] Paste a matrix column in pairwise fashion with other columns?
#Hello, #I have would like to paste a single column of a matrix # in pair wise fashion with other columns based upon # even and odd column numbers. # I can do it in a very clunky fashion and I know there # must be a better way. below is a sample matrix and my extremely # clunky code that gets the job done for a small matrix, but i plan to # do this on a much grander scale. any help would be very much appreciated. 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 paste column 1 with every odd column # I would like to paste column 2 with every even column #I know i can do it step by step in this fashion Column.1.2_by_Column.3.4<-paste(P.genotype.sample[,1:2],P.genotype.sample [,3:4]) Column.1.2_by_Column.3.4 Column.1.2_by_Column.5.6<-paste(P.genotype.sample[,1:2],P.genotype.sample [,5:6]) Column.1.2_by_Column.5.6 Column.1.2_by_Column.7.8<-paste(P.genotype.sample[,1:2],P.genotype.sample [,7:8]) Column.1.2_by_Column.7.8 Column.1.2_by_Column.9.10<-paste(P.genotype.sample[,1:2],P.genotype.sample [,9:10]) Column.1.2_by_Column.9.10 Column.1.2.data.matrix<- matrix(20,20,4) Column.1.2.data.list<-list(Column.1.2_by_Column.3.4,Column.1.2_by_Column.5.6 , Column.1.2_by_Column.7.8,Column.1.2_by_Column.9.10) for (i in 1:4){ Column.1.2.data.matrix[,i]<-Column.1.2.data.list[[i]] } Column.1.2.data.matrix # However i will have many more data columns to compare to in a larger data set # and I was wondering if there was a more clever way to do this and still # come up with the same result. # Any help would be greatly appreciated # Thanks in advance Luke Neraas lukasneraas.r@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]]
jim holtman
2007-Sep-26 01:05 UTC
[R] Paste a matrix column in pairwise fashion with other columns?
try this:> 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[,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 matrix of odd/even indices > x <- matrix(3:ncol(P.genotype.sample), byrow=TRUE, ncol=2) > # now create the desired output > apply(x, 1, function(.row){+ paste(P.genotype.sample[,1:2], P.genotype.sample[,.row]) + }) [,1] [,2] [,3] [,4] [1,] "2 2" "2 3" "2 1" "2 5" [2,] "2 2" "2 3" "2 5" "2 5" [3,] "1 2" "1 8" "1 5" "1 8" [4,] "5 3" "5 3" "5 1" "5 5" [5,] "1 3" "1 6" "1 5" "1 5" [6,] "1 2" "1 1" "1 1" "1 5" [7,] "5 2" "5 1" "5 1" "5 5" [8,] "6 2" "6 1" "6 5" "6 2" [9,] "1 3" "1 1" "1 5" "1 5" [10,] "3 3" "3 3" "3 5" "3 2" [11,] "6 2" "6 6" "6 5" "6 5" [12,] "3 8" "3 3" "3 5" "3 8" [13,] "3 8" "3 8" "3 5" "3 8" [14,] "6 3" "6 8" "6 5" "6 8" [15,] "8 8" "8 6" "8 7" "8 8" [16,] "1 2" "1 8" "1 6" "1 5" [17,] "6 8" "6 7" "6 7" "6 8" [18,] "7 3" "7 3" "7 5" "7 5" [19,] "2 4" "2 1" "2 5" "2 8" [20,] "3 3" "3 7" "3 8" "3 2">On 9/25/07, Luke Neraas <lukasneraas.r at gmail.com> wrote:> #Hello, > > #I have would like to paste a single column of a matrix > # in pair wise fashion with other columns based upon > # even and odd column numbers. > # I can do it in a very clunky fashion and I know there > # must be a better way. below is a sample matrix and my extremely > # clunky code that gets the job done for a small matrix, but i plan to > # do this on a much grander scale. any help would be very much appreciated. > > 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 paste column 1 with every odd column > # I would like to paste column 2 with every even column > > #I know i can do it step by step in this fashion > > Column.1.2_by_Column.3.4<-paste(P.genotype.sample[,1:2],P.genotype.sample > [,3:4]) > Column.1.2_by_Column.3.4 > > Column.1.2_by_Column.5.6<-paste(P.genotype.sample[,1:2],P.genotype.sample > [,5:6]) > Column.1.2_by_Column.5.6 > > Column.1.2_by_Column.7.8<-paste(P.genotype.sample[,1:2],P.genotype.sample > [,7:8]) > Column.1.2_by_Column.7.8 > > Column.1.2_by_Column.9.10<-paste(P.genotype.sample[,1:2],P.genotype.sample > [,9:10]) > Column.1.2_by_Column.9.10 > > Column.1.2.data.matrix<- matrix(20,20,4) > > Column.1.2.data.list<-list(Column.1.2_by_Column.3.4,Column.1.2_by_Column.5.6 > , > Column.1.2_by_Column.7.8,Column.1.2_by_Column.9.10) > for (i in 1:4){ > Column.1.2.data.matrix[,i]<-Column.1.2.data.list[[i]] > } > Column.1.2.data.matrix > > # However i will have many more data columns to compare to in a larger data > set > # and I was wondering if there was a more clever way to do this and still > # come up with the same result. > > # Any help 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?