Do a loop with letters in R -
i want loop letter..i have matrix(named 'a') , want have column names..
k<-arrayind(2,dim(a)) colnames(a)[k[,1]] colnames(a)[k[,2]] colnames(a)[k[,3]] . . . colnames(a)[k[,n]]
i guess loop that
aa<-list() (i in 1:n) { aa[[i]]<-colnames(a)[k[,i]] }
but don't results. think loop ok have change else the
aa<-list()
and replace "list" else..
suppose have matrix mat
, looks this:
mat <- matrix(1:4, ncol = 2, dimnames = list(letters[1:2], letters[1:2]))
you can inspect structure this:
str(mat) # int [1:2, 1:2] 1 2 3 4 # - attr(*, "dimnames")=list of 2 # ..$ : chr [1:2] "a" "b" # ..$ : chr [1:2] "a" "b"
and column names using this:
colnames(mat) # [1] "a" "b"