Skip to content
Prev 180188 / 398506 Next

Looking for a quick way to combine rows in a matrix

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,] <-
colSums(c[i:I(i-1),])
              c[i-1,]<-NA}} # sums the rows and replace the used rows by NA
values
c <- c[apply(c,1,function(x)any(!is.na(x))),] # removes the rows with NA
values
c

Rock
Rocko22 wrote: