[R] using for variable as rowname

(Ted Harding) Ted.Harding at manchester.ac.uk
Wed Sep 17 17:02:22 CEST 2008


On 17-Sep-08 14:22:11, Ralikwen wrote:
> 
> Is there a way to use the cycle variable for rowname?
> 
> v=1:6
> for (a in 1:3){
>  for  (b in 4:5) {
>   v=rbind(v,a.b=1)
>  }
> }
> v
> 
> This above obviously does not work, but I couldn't find out how to use
> a and
> b to construct a rowname like 14, 15, 24, 25.
> Thanks for the help.
> Balazs

I don't know of a way to construct a paramater-name out of variable values
(so that "a.b" is a dynamic name as intended in your v=rbind(v,a.b=1) ),
though possibly someone else does!

However, something along the following lines would work (you construct
a vector of row names, along with v, within the loop, and then apply
it at the end):

  v<-(1:6)
  a.b<-"0"
  for (a in 1:3){
   for  (b in 4:5) {
    a.b<-c(a.b,paste(a,".",b,sep=""))
    v=rbind(v,1)
   }
  }
  rownames(v)<-a.b
  v
#     [,1] [,2] [,3] [,4] [,5] [,6]
# 0      1    2    3    4    5    6
# 1.4    1    1    1    1    1    1
# 1.5    1    1    1    1    1    1
# 2.4    1    1    1    1    1    1
# 2.5    1    1    1    1    1    1
# 3.4    1    1    1    1    1    1
# 3.5    1    1    1    1    1    1

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 17-Sep-08                                       Time: 16:02:18
------------------------------ XFMail ------------------------------



More information about the R-help mailing list