0

I have got the decision tree for churn data set using J48()function from RWeka package. The tree is really big hence I am unable to see the whole tree. I want to output it in a text file but the format is getting changed. How can I save it preserving the tree format.

save(m2,file="thisexample.txt", ascii=TRUE)

m2 is the dataframe in which I am storing the J48 tree output.

Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30
Joe
  • 183
  • 5
  • 16
  • As a text file? What exactly would that look like? It would help if your example were [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with some sample input so we can see what you are doing. – MrFlick Aug 03 '16 at 19:08
  • I just want it in a format which i can open and see the tree structure as the output in dataframe is too big to come in a single screen in R. On page 4 of this link, my output also looks like it - https://www.erpublication.org/admin/vol_issue1/upload%20Image/IJETR032129.pdf – Joe Aug 03 '16 at 19:12

1 Answers1

3

I. Example with iris data set using RWeka's J48() function.

      library(RWeka)
      result = J48(Species~.,data=iris)
      result
      # J48 pruned tree
      # ------------------

      # Petal.Width <= 0.6: setosa (50.0)
      # Petal.Width > 0.6
      # |   Petal.Width <= 1.7
      # |   |   Petal.Length <= 4.9: versicolor (48.0/1.0)
      # |   |   Petal.Length > 4.9
      # |   |   |   Petal.Width <= 1.5: virginica (3.0)
      # |   |   |   Petal.Width > 1.5: versicolor (3.0/1.0)
      # |   Petal.Width > 1.7: virginica (46.0/1.0)

      # Number of Leaves  :     5

      # Size of the tree :      9

II. Use sink() function to write it in a text file

      sink("result.txt")
      print (result)
      sink()

III. Open result.txt saved in your current working directory.

enter image description here

Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30