[R] Looking for a quick way to combine rows in a matrix
Rocko22
rock.ouimet at gmail.com
Wed May 13 02:53:05 CEST 2009
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 elegant and
straightforward, but it works:
Let's try with a matrix "b" that contains more rows than in your example:
b=matrix(1:32, ncol=4)
rownames(b)=rep(c("AA","AT","TA","TT"),2)
key <- rownames(b)
key[key == "AT"] <- "TA"
rownames(b)=key
for(i in 1:I(nrow(b)-1)) {
if(rownames(b)[i]=="TA" & rownames(b)[i+1]=="TA") { b[i,] <-
colSums(b[i:I(i+1),])
b[i+1,]<-NA}} # sums the rows and replace the used rows by NA
values
b <- b[order(b[,1],na.last=NA),] # removes the rows with NA values
Of course, the rows are reordered, and that may be not wanted. The ordering
was just to remove the NA rows.
Rock :-D
--
View this message in context: http://www.nabble.com/Looking-for-a-quick-way-to-combine-rows-in-a-matrix-tp23491348p23513599.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list