Jim, thank you, that worked great.

Paul Prew  |  Statistician
651-795-5942   |   fax 651-204-7504 
Ecolab Research Center  | Mail Stop ESC-F4412-A 
655 Lone Oak Drive  |  Eagan, MN 55121-1560 

-----Original Message-----
From: Jim Lemon [mailto:[email protected]] 
Sent: Friday, February 22, 2013 7:22 PM
To: Prew, Paul
Cc: [email protected]
Subject: Re: [R] assign index to colnames(matrix)

On 02/23/2013 11:34 AM, Prew, Paul wrote:
> Hello,  I’m trying to follow the syntax of a script from a journal 
> website.  In order to create a regression formula used later in the 
> script, the regression matrix must have column names “X1”, 
> “X2”, etc.  I have tried to assign these column names to my matrix 
> ScoutRSM.mat using a for loop, but I don’t know how to interpret the 
> error message.  Suggestions?  Thanks, Paul
>
>
> ====================================================
> ====== instructions about dimnames(X)[[2]] = "X1","X2",...  ======= 
> ====================================================
>
> function(binstr)
> {
>    # makes formula for regression
>    # must make sure dimnames(X)[[2]] = "X1","X2",...
>    ind<-which(binstr==1)
>    rht<- paste("X", ind, sep="", collapse="+")
>    ans<-paste("y ~ ", rht)
>    return(as.formula(ans))
> }
>
>
> #######################################
> ##############  my for loop  ############## 
> #######################################
>
> for (i in 1:dim(ScoutRSM.mat)[2]  {
> colnames(ScoutRSM.mat)[i]<- paste("X", i, sep = “”)) }
>
>> for(i in 1:dim(ScoutRSM.mat)[2]) {
> +   colnames(ScoutRSM.mat)[i]<- paste("X",i, sep = "") }
> Error in `colnames<-`(`*tmp*`, value = "X1") :
>    length of 'dimnames' [2] not equal to array extent
>
Hi Paul,
You don't really need the loop, try:

colnames(ScoutRSM.mat)<-paste("X",1:dim(ScoutRSM.mat)[2],sep="")

Jim

CONFIDENTIALITY NOTICE: 
This e-mail communication and any attachments may contain proprietary and 
privileged information for the use of the designated recipients named above. 
Any unauthorized review, use, disclosure or distribution is prohibited. 
If you are not the intended recipient, please contact the sender by reply 
e-mail and destroy all copies of the original message.

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to