[R] variable names

Torsten Hothorn hothorn at ci.tuwien.ac.at
Tue Jul 22 16:16:59 CEST 2003


On Tue, 22 Jul 2003, Luis Miguel Almeida da Silva wrote:

> I didn't noticed that fact. I've already found a way to do that
>  
> x <- 1:40
> colnames(df.treino) <- paste("Ncp",x,sep=".")
>  
> and this generates names that I can relate with the variables. Thanks anyway
>  
> The problem is that I use rpart in a loop and the class labels are in the last column. For the above example I would "type"
>  
> rpart(Ncp.40~.,data=df.treino)
>  
> But in the next step of the loop I can have only 35 variables and the class labels would be at the Ncp.36. So I have to refresh the formula in rpart... and that is my problem
> 

R> df.treino <- data.frame(matrix(rnorm(25), ncol=5))
R> thisformula <- as.formula(paste(colnames(df.treino)[ncol(df.treino)],
"~ ."))
R> thisformula
X5 ~ .
R> rpart(thisformula, data = df.treino)
n= 5 

node), split, n, deviance, yval
      * denotes terminal node

1) root 5 3.032904 -0.3392065 *

Torsten


> 	-----Original Message----- 
> 	From: Torsten Hothorn [mailto:hothorn at ci.tuwien.ac.at] 
> 	Sent: Tue 22/07/2003 14:57 
> 	To: Luis Miguel Almeida da Silva 
> 	Cc: r-help at stat.math.ethz.ch 
> 	Subject: Re: [R] variable names
> 	
> 	
> 
> 
> 	On Tue, 22 Jul 2003, Luis Miguel Almeida da Silva wrote:
> 	
> 	> Dear helpers
> 	> 
> 	> I want to use rpart several times in a loop to build a classification tree. My problem is that rpart needs a formula as argument and for that the variables need to have names and this doesn't happen in my case. Every iteration in the loop has a different dataset with several variables (ex. 38 or more) and so I can't type the names by hand every time. Is there any function that generates names for variables in a dataframe. If so, how can I use then the argument
> 	> 
> 	
> 	If your data is organised in a data.frame, (dummy) variable names are
> 	available by default:
> 	
> 	R> mydata <- data.frame(matrix(rnorm(25), ncol=5))
> 	R> mydata
> 	          X1          X2         X3         X4          X5
> 	1  1.3806313 -0.41827136  0.9591628 -1.3351038  0.02746110
> 	2  0.5114590 -1.34111439 -0.9617552 -0.8367088 -0.06913021
> 	3 -1.7508089 -0.49387076 -1.7597395  2.3899490 -0.15209650
> 	4 -1.6753809 -1.28381808 -1.0424903  0.1002998  0.27784949
> 	5 -0.2605535 -0.09035652 -2.5786418  1.0483400 -0.70445615
> 	R> rpart(X1 ~ ., data = mydata)
> 	n= 5
> 	
> 	node), split, n, deviance, yval
> 	      * denotes terminal node
> 	
> 	1) root 5 7.463698 -0.3589306 *
> 	
> 	best,
> 	
> 	Torsten
> 	
> 	> rpart(classlabels~. ,.....)
> 	> 
> 	> thanks
> 	>
> 	> ______________________________________________
> 	> R-help at stat.math.ethz.ch mailing list
> 	> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 	>
> 	>
> 	
> 	
> 
>




More information about the R-help mailing list