[R] assign index to colnames(matrix)

PIKAL Petr petr.pikal at precheza.cz
Mon Feb 25 07:59:56 CET 2013


Hi

What language are you comming from?

colnames(ScoutRSM.mat) <- paste("X", 1:39, sep="")

Regards
Petr


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Prew, Paul
> Sent: Saturday, February 23, 2013 1:35 AM
> To: r-help at r-project.org
> Subject: [R] assign index to colnames(matrix)
> 
> 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
> 
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ~~~~~~~~  ScoutRSM.mat  ~~~~~~~~~~
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> > dput(ScoutRSM.mat)
> structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1,
> 1, -1, 0, -1, 0, 0, 0, 1, 1, 0, 1, -1, 1, -1, 1, 0, -1, -0.464, -0.516,
> -0.48, 1, -0.008, 0.486, 0, 0.524, -0.96, -1, -0.526, 1, 0.47, 0.49, 0,
> 0.53, -0.866, -0.6, 0.227, -1, -0.895, 1.289, 0.181, 0.204, -1, 0.199,
> -0.908, -1, -1, 1.289, 1.289, 0.204, -1, 0.213, -0.922, -1, -0.84, -1,
> 0.2, -1, -1, 0.05, -1, 1.136, -0.861, -0.861, -1, 1.136, 0.193, 0.047,
> -1, -1, -0.733, -1, -0.817, 0.14, -1, 1, 0.127, -0.05, 1, -1, -0.797, -
> 0.797, 0.123, -1, -1, -0.04, 1, 1, -0.75, 0, 0.464, -0.516, 0.48, 0,
> 0.008, 0, 0, 0, -0.96, -1, 0, 1, -0.47, 0.49, 0, 0.53, 0, 0.6, -0.227,
> -1, 0.895, 0, -0.181, 0, 0, 0, -0.908, -1, 0, 1.289, -1.289, 0.204, 1,
> 0.213, 0, 1, 0.84, -1, -0.2, 0, 1, 0, 0, 0, -0.861, -0.861, 0, 1.136, -
> 0.193, 0.047, 1, -1, 0, 1, 0.817, 0.14, 1, 0, -0.127, 0, 0, 0, -0.797,
> -0.797, 0, -1, 1, -0.04, -1, 1, 0, 0, -0.105, 0.516, 0.429, 1.289, -
> 0.001, 0.099, 0, 0.104, 0.872, 1, 0.526, 1.289, 0.606, 0.1, 0, 0.113,
> 0.799, 0.6, 0.39, 0.516, -0.096, -1, 0.008, 0.025, 0, 0.596, 0.827,
> 0.861, 0.526, 1.136, 0.091, 0.023, 0, -0.53, 0.635, 0.6, 0.379, -0.072,
> 0.48, 1, -0.001, -0.024, 0, -0.524, 0.765, 0.797, -0.065, -1, -0.47, -
> 0.02, 0, 0.53, 0.65, 0, -0.19, 1, -0.179, -1.289, -0.181, 0.01, 1,
> 0.226, 0.782, 0.861, 1, 1.464, 0.249, 0.01, 1, -0.213, 0.676, 1, -
> 0.185, -0.14, 0.895, 1.289, 0.023, -0.01, -1, -0.199, 0.724, 0.797, -
> 0.123, -1.289, -1.289, -0.008, -1, 0.213, 0.692, 0, 0.686, -0.14, -0.2,
> -1, -0.127, -0.003, -1, -1.136, 0.686, 0.686, -0.123, -1.136, -0.193, -
> 0.002, -1, -1, 0.55, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1,
> 0, 1, 0.215, 0.266, 0.23, 1, 0, 0.236, 0, 0.275, 0.922, 1, 0.277, 1,
> 0.221, 0.24, 0, 0.281, 0.75, 0.36, 0.051, 1, 0.801, 1.66, 0.033, 0.042,
> 1, 0.04, 0.825, 1, 1, 1.66, 1.66, 0.042, 1, 0.045, 0.85, 1, 0.705, 1,
> 0.04, 1, 1, 0.003, 1, 1.292, 0.742, 0.742, 1, 1.292, 0.037, 0.002, 1,
> 1, 0.537, 1, 0.667, 0.02, 1, 1, 0.016, 0.003, 1, 1, 0.635, 0.635,
> 0.015, 1, 1, 0.002, 1, 1, 0.563, 0, 0.105, 0.516, -0.429, 0, 0.001, 0,
> 0, 0, 0.872, 1, 0, 1.289, -0.606, 0.1, 0, 0.113, 0, -0.6, -0.39, 0.516,
> 0.096, 0, -0.008, 0, 0, 0, 0.827, 0.861, 0, 1.136, -0.091, 0.023, 0, -
> 0.53, 0, -0.6, -0.379, -0.072, -0.48, 0, 0.001, 0, 0, 0, 0.765, 0.797,
> 0, -1, 0.47, -0.02, 0, 0.53, 0, 0, 0.19, 1, 0.179, 0, 0.181, 0, 0, 0,
> 0.782, 0.861, 0, 1.464, -0.249, 0.01, -1, -0.213, 0, -1, 0.185, -0.14,
> -0.895, 0, -0.023, 0, 0, 0, 0.724, 0.797, 0, -1.289, 1.289, -0.008, 1,
> 0.213, 0, 0, -0.686, -0.14, 0.2, 0, 0.127, 0, 0, 0, 0.686, 0.686, 0, -
> 1.136, 0.193, -0.002, 1, -1, 0, 0, 0.088, -0.516, 0.086, -1.289, 0.001,
> 0.005, 0, 0.119, -0.751, -0.861, -0.526, 1.464, 0.117, 0.005, 0, -
> 0.113, -0.585, -0.6, 0.086, 0.072, -0.429, 1.289, 0, -0.005, 0, -0.104,
> -0.695, -0.797, 0.065, -1.289, -0.606, -0.004, 0, 0.113, -0.599, 0, -
> 0.318, 0.072, 0.096, -1, 0.001, -0.001, 0, -0.596, -0.659, -0.686,
> 0.065, -1.136, -0.091, -0.001, 0, -0.53, -0.476, 0, 0.155, 0.14, 0.179,
> -1.289, -0.023, -0.001, 1, -0.226, -0.623, -0.686, 0.123, -1.464, -
> 0.249, 0, 1, -0.213, -0.507, 0, -0.464, -0.516, -0.48, 0, -0.008, 0, 0,
> 0, -0.96, -1, 0, 1, 0.47, 0.49, 0, 0.53, 0, -0.6, 0.227, -1, -0.895, 0,
> 0.181, 0, 0, 0, -0.908, -1, 0, 1.289, 1.289, 0.204, -1, 0.213, 0, -1, -
> 0.84, -1, 0.2, 0, -1, 0, 0, 0, -0.861, -0.861, 0, 1.136, 0.193, 0.047,
> -1, -1, 0, -1, -0.817, 0.14, -1, 0, 0.127, 0, 0, 0, -0.797, -0.797, 0,
> -1, -1, -0.04, 1, 1, 0, 0, -0.215, 0.266, -0.23, 0, 0, 0, 0, 0, 0.922,
> 1, 0, 1, -0.221, 0.24, 0, 0.281, 0, -0.36, -0.051, 1, -0.801, 0, -
> 0.033, 0, 0, 0, 0.825, 1, 0, 1.66, -1.66, 0.042, -1, 0.045, 0, -1, -
> 0.705, 1, -0.04, 0, -1, 0, 0, 0, 0.742, 0.742, 0, 1.292, -0.037, 0.002,
> -1, 1, 0, -1, -0.667, 0.02, -1, 0, -0.016, 0, 0, 0, 0.635, 0.635, 0, 1,
> -1, 0.002, -1, 1, 0, 0), .Dim = c(18L, 39L), .Dimnames =
> list(c("ScoutRSM.mat", "", "", "", "", "", "", "", "", "", "", "", "",
> "", "", "", "", ""), NULL))
> 
> 
> 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
> 
> 
> 
> 
> 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.
> 
> 
> 	[[alternative HTML version deleted]]



More information about the R-help mailing list