matrix - define column names as combination of a retpeted string and a vector in R -


i want name columns of matrix using string , vector of length of rows. example:

 k<-c(5:15)  xmin = 3  xmax = 15  x<-c(xmin:xmax)  m<-matrix(, nrow = length(x), ncol = length(k)) 

ideally, name matrix columns using string , vector k, column name of i'th column same item @ i'th position of vector k.

so, this:

      s5 s6 s7 s8 s9 s10 s11 s12 s13 s14 s15  [1,] na na na na na na na na na na na  [2,] na na na na na na na na na na na  [3,] na na na na na na na na na na na  [4,] na na na na na na na na na na na  [5,] na na na na na na na na na na na  [6,] na na na na na na na na na na na  [7,] na na na na na na na na na na na  [8,] na na na na na na na na na na na  [9,] na na na na na na na na na na na [10,] na na na na na na na na na na na [11,] na na na na na na na na na na na [12,] na na na na na na na na na na na [13,] na na na na na na na na na na na 

where string "s". couriously, didn't work was:

n=null n<- as.vector(n) m=1 (m in length(k)) { n[m]<-paste0("s_",k[m]) } n 

=> output vector full of na's! although, when iterated 'm' hand, worked (why not working in loop?)!

whatever, wanted get, of course:

> n  [1] "s_5"  "s_6"  "s_7"  "s_8"  "s_9"  "s_10" "s_11" "s_12"  [9] "s_13" "s_14" "s_15" 

which use for:

colnames(m)<-n 

i of course want use same procedure naming rows.

you don't need loop this. paste vectorized.

k = seq(from = 5, = 15) paste("s", k, sep = "_") 

or

paste0("s_", k) 

however, if curious why loop fails because aren't looping on besides result of length. hence na values in other vector elements. need have iterate on loop. in case use simple seq(length(k)) or seq_along.

for (m in seq_along(k)) {   n[m]<-paste0("s_",k[m]) } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -