Re: [R] Looking for a quick way to combine rows in a matrix

2009-05-13 Thread Rocko22
Hello, I reviewed my code and this will work now for any number of successive TA, I hope: b=matrix(1:64, ncol=4) rownames(b)=rep(c(AA,AT,TA,TT),each=4) key - rownames(b) key[key == AT] - TA c - b rownames(c)=key for(i in 2:I(nrow(c))) { if(rownames(c)[i]==TA rownames(c)[i-1]==TA) { c[i,] -

Re: [R] Looking for a quick way to combine rows in a matrix

2009-05-13 Thread Chris Stubben
You can automate this step key[key == AT] - TA ## create a function to reverse a string -- see strsplit help page for this strReverse function reverse - function(x) sapply(lapply(strsplit(x, NULL), rev), paste, collapse=) key - rownames(a) # combine rownames with reverse (rownames) n-cbind(key,

Re: [R] Looking for a quick way to combine rows in a matrix

2009-05-12 Thread Johannes Hüsing
jim holtman schrieb: Try this: key - rownames(a) key[key == AT] - TA do.call(rbind, by(a, key, colSums)) something like paste(sort(strsplit(key, split=)[[1]]), ) might be more general. __ R-help@r-project.org mailing list

Re: [R] Looking for a quick way to combine rows in a matrix

2009-05-12 Thread Rocko22
In the first reply, what was calculated was the overall means by group (amino acids). It does not work for a larger database. I am quite really new to R, and I worked on your question just to learn how to manipulate data with R. The following seems to work. The code could be made a lot more

Re: [R] Looking for a quick way to combine rows in a matrix

2009-05-12 Thread Jorge Ivan Velez
Dear Jacy, If AT and TA always one after the other, you might consider the following as an alternative: res - apply(a, 2, function(x) c(x[1], sum(x[2:3]), x[4] )) rownames(res) - rownames(a)[-3] res #[,1] [,2] [,3] [,4] #AA159 13 #AT5 13 21 29 #TT48 12 16 HTH,

[R] Looking for a quick way to combine rows in a matrix

2009-05-11 Thread Crosby, Jacy R
I'm working with genotype data in a frequency table: a=matrix(1:16, nrow=4) rownames(a)=c(AA,AT,TA,TT) a [,1] [,2] [,3] [,4] AA159 13 AT26 10 14 TA37 11 15 TT48 12 16 'AT' and 'TA' are essentially the same, and I'd like to combine (add) the

Re: [R] Looking for a quick way to combine rows in a matrix

2009-05-11 Thread jim holtman
Try this: key - rownames(a) key[key == AT] - TA do.call(rbind, by(a, key, colSums)) V2 V3 V4 V5 AA 1 5 9 13 TA 5 13 21 29 TT 4 8 12 16 On Mon, May 11, 2009 at 4:53 PM, Crosby, Jacy R jacy.r.cro...@uth.tmc.eduwrote: I'm working with genotype data in a frequency table: