peter eric
2005-Dec-10 17:17 UTC
[R] how to extract the row names of a matrix using for loop or other looping
Hi all, I have a matrix and its cluster...and i like to extract the row names of each cluster....but using the for loop or some other looping a<-matrix(c(seq(3,45,3),seq(10,6,-1)),4,5,byrow=F) col<-c("ra","rb","rc","rd","re") rows<-c("ca","cb","cc","cd") dimnames(a)<-list(rows,col) a ## matrix #----------------------------------------------------------------------------------- # ra rb rc rd re # ca 3 15 27 39 9 # cb 6 18 30 42 8 # cc 9 21 33 45 7 # cd 12 24 36 10 6 ------------------------------------------------------------------------------------ t<-list() ## clusters of the matrix s<-list() r<-c(1:3) for(i in 1:3) { p<-seq(r[i],4,3) s[[i]]<-print(p) t[[i]]<-a[s[[i]],,drop=FALSE] } print(t) ---------------------------------------------------------------------- ##[[1]] ra rb rc rd re ca 3 15 27 39 9 cd 12 24 36 10 6 [[2]] ra rb rc rd re cb 6 18 30 42 8 [[3]] ra rb rc rd re cc 9 21 33 45 7 ---------------------------------------------------------------------------------------------------- ##now if i want to extract the row names i can do it manually giving the values like rowc <- list(c(rownames(t[[1]])), c(rownames(t[[2]])), rownames(t[[3]])) but i like to do using for loop or other loops...could you give me some suggestion..i伌ve tried like this..but it is not working.... rowc<-list(for(i in 1:3){p<-c();k<-rownames(t[[i]]);p[[i]]<-print(k)}) ## here I伌m getting only the final rowname t[[3]]..other two values are missing..how could i do this to store all the values........... thank you... with regards, eric. --------------------------------- [[alternative HTML version deleted]]
Uwe Ligges
2005-Dec-10 17:37 UTC
[R] how to extract the row names of a matrix using for loop or other looping
peter eric wrote:> Hi all, > I have a matrix and its cluster...and i like to extract the row names of each cluster....but using the for loop or some other looping > > a<-matrix(c(seq(3,45,3),seq(10,6,-1)),4,5,byrow=F) > col<-c("ra","rb","rc","rd","re") > rows<-c("ca","cb","cc","cd") > dimnames(a)<-list(rows,col) > a ## matrix > #----------------------------------------------------------------------------------- > # ra rb rc rd re > # ca 3 15 27 39 9 > # cb 6 18 30 42 8 > # cc 9 21 33 45 7 > # cd 12 24 36 10 6 > ------------------------------------------------------------------------------------ > > t<-list() ## clusters of the matrix > s<-list() > r<-c(1:3) > for(i in 1:3) > { p<-seq(r[i],4,3) > s[[i]]<-print(p) > t[[i]]<-a[s[[i]],,drop=FALSE] > } > print(t) > ---------------------------------------------------------------------- > ##[[1]] > ra rb rc rd re > ca 3 15 27 39 9 > cd 12 24 36 10 6 > [[2]] > ra rb rc rd re > cb 6 18 30 42 8 > [[3]] > ra rb rc rd re > cc 9 21 33 45 7 > ---------------------------------------------------------------------------------------------------- > > > ##now if i want to extract the row names i can do it manually giving the values like > > rowc <- list(c(rownames(t[[1]])), c(rownames(t[[2]])), rownames(t[[3]])) > > > but i like to do using for loop or other loops...could you give me some suggestion..i??ve tried like this..but it is not working.... > > rowc<-list(for(i in 1:3){p<-c();k<-rownames(t[[i]]);p[[i]]<-print(k)}) > > ## here I??m getting only the final rowname t[[3]]..other two values are missing..how could i do this to store all the values...........Please teach your mail software to wrap lines. Please do not send HTML mail. You are looking for: rowc <- lapply(t, rownames) A corresponding loop (which you should not use) would be: rowc <- vector(mode = "list", length = length(t)) for(i in seq(along = t)) rowc[[i]] <- rownames(t[[i]]) BTW: t is a function in R, it is not very nice to use names twice ... Uwe Ligges> thank you... > > with regards, > eric. > > > --------------------------------- > > > [[alternative HTML version deleted]] > > > > ------------------------------------------------------------------------ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html