[R] Convert numeric to class

Liaw, Andy andy_liaw at merck.com
Mon Mar 21 16:15:37 CET 2005



> -----Original Message-----
> From: Xiyan Lon
> 
> Dear all,
> I have a script about iteration classification, like this below
> 
> data(iris)
>   N <- 5
>   ir.tr.iter <- vector('list',N)
>   ir.tr <- vector('list',N)
>   for (j in 1:N) {
>     ir.tr[[j]] <- rpart(Species ~., data=iris)
>     ir.tr.iter[j] <- ir.tr[[j]]$frame
>     result  <- list(ir.tr=ir.tr, ir.tr.iter=ir.tr.iter)
>   }        
> 
> as.data.frame(as.matrix(ir.tr.iter))
> 
> Because I need the result as dataframe I convert to as.matrix,

The `frame' component of an rpart object should already be a data frame.
You just need to make sure to assign them to components of ir.tr.iter, as,
e.g., 

  ir.tr.iter[[j]] <- ir.tr[[j]]$frame

BTW, why would you want to duplicate the data in `result'?  You can easily
get the `frame' component from ir.tr, so why store a separate copy?

Andy


> but all it only numeric
> > as.data.frame(as.matrix(ir.tr.iter))
>              V1
> 1 4, 1, 5, 1, 1
> 2 4, 1, 5, 1, 1
> 3 4, 1, 5, 1, 1
> 4 4, 1, 5, 1, 1
> 5 4, 1, 5, 1, 1
> 
> 
> > ir.tr.iter
> [[1]]
> [1] Petal.Length <leaf>       Petal.Width  <leaf>       <leaf>      
> Levels: <leaf> Sepal.Length Sepal.Width Petal.Length Petal.Width
> .
> .
> .
> [[5]]
> [1] Petal.Length <leaf>       Petal.Width  <leaf>       <leaf>      
> Levels: <leaf> Sepal.Length Sepal.Width Petal.Length Petal.Width
> 
> 
> I don't know where are 1, 4 and 5 (numeric) come from, but I guest 1
> (<leaf>), 4 (Petal.Length) and 5 (Petal.Width).
> I want to convert the species (class/type) like,
>                                                               
>           V1
> Petal.Length <leaf>       Petal.Width  <leaf>       <leaf>
> Petal.Length <leaf>       Petal.Width  <leaf>       <leaf>
> Petal.Length <leaf>       Petal.Width  <leaf>       <leaf>
> Petal.Length <leaf>       Petal.Width  <leaf>       <leaf>
> Petal.Length <leaf>       Petal.Width  <leaf>       <leaf>
> 
> How can I do?
> 
> Thanks in advance.
> 
> Xiyan Lon
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
> 
>




More information about the R-help mailing list