[R] exact the variables used in tree construction

David Winsemius dwinsemius at comcast.net
Wed May 12 23:52:47 CEST 2010


On May 12, 2010, at 5:31 PM, Changbin Du wrote:

>> fit.dimer <- rpart(as.factor(out) ~ ., method="class", data=p_df)
>>
>> fit.dimer$frame[, "var"]
> [1] NE     WC     <leaf> TA     <leaf> <leaf> WG     WD     WW     WC
> [11] <leaf> <leaf> <leaf> CT     <leaf> FC     <leaf> YG     QT      
> <leaf>
> [21] <leaf> <leaf> NW     DP     DY     <leaf> SK     <leaf> <leaf>  
> <leaf>
> [31] <leaf>
> 401 Levels: <leaf> AA AC AD AE AF AG AH AI AK AL AM AN AP AQ AR AS  
> AT AV ...
> YY
>>
>> at<-grep("<leaf>", fit.dimer$frame[, "var"], value=FALSE,
> ignore.case=TRUE)
>>
>> fit.dimer$frame[-at, "var"]
> [1] NE WC TA WG WD WW WC CT FC YG QT NW DP DY SK
> 401 Levels: <leaf> AA AC AD AE AF AG AH AI AK AL AM AN AP AQ AR AS  
> AT AV ...
> YY
>>
>> unique(fit.dimer$frame[-at, "var"])
> [1] NE WC TA WG WD WW CT FC YG QT NW DP DY SK
> 401 Levels: <leaf> AA AC AD AE AF AG AH AI AK AL AM AN AP AQ AR AS  
> AT AV ...
> YY
>
>
> HI, Dear R community,
>
> I tried to exact the variables used in tree construction, I used above
> codes. Is there a way to remove the "401 levels......"?

Are you trying to get the number of levels to equal the present set,  
then:

?factor

 > af <- factor(letters[1:10] ,levels=letters[1:20])
 > af
  [1] a b c d e f g h i j
Levels: a b c d e f g h i j k l m n o p q r s t
 > factor(af)
  [1] a b c d e f g h i j
Levels: a b c d e f g h i j

If you are trying to convert to a vector then:

?as.character

-- 
David.

>
> How to add " "  around each variables? I want them to become "NW",  
> "WC",
> etc.
>
> Thanks so much!
>
>
> -- 
> Sincerely,
> Changbin
> --
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list