Search code examples
rplotstem

How to create a stem-leaf plot in R from txt file?


I am BRAND new to R. I need help creating a simple stem-leaf plot in R. This is my data to create the stem-leaf plot. It is saved in an text file.

THIS IS HOW I WANT MY STEM TO LOOK-LEAF LIKE.

Stem Leaf
0    1 1 2 2 3 3 4 4 5 5 5 5 6 6 6 7 8 8 8 8 9 9 9 
1    0 0 0 1 2 2 2 2 2 2 3 3 5 6 6 8 9 9 9
2    0 1 2 3 3 4 5 5 6 6 7 8
3    0 0 0 2 3 4 7 8 
4    3 3 4 4 6 8 
5    1 2 5
6    1 5
7    7

Now i load that into "R" and read its data but when I run view the table...this is how it looks like.

enter image description here

Its not showing how it suppose to show. So to make the stem-leaf plot wrote the following code.

data2 <- read.csv("C:/Users/jaina/Desktop/question2.txt", header = T)
stem(data2$Leaf)

The above code gives me the error of Error in stem(data2$Leaf) : 'x' must be numeric

So can someone help me solve this thing and display the proper stem-leaf plot.

Thank you.

DATA:

01,01,02,02,03,03,04,04,05,05,05,05,06,06,06,07,08,08,08,08,09,09,09,10,10,10,11,12,12,12,12,12,12,13,13,15,16,16,18,19,19,19,20,21,22,23,23,24,25,25,26,26,27,28,30,30,30,31,32,33,34,37,38,43,43,44,44,46,48,51,52,55,61,65,67

Solution

  • data2 <- data.frame(Leaf=c(01,01,02,02,03,03,04,04,05,05,05,05,06,06,06,07,08,08,08,08,09,
                               09,09,10,10,10,11,12,12,12,12,12,12,13,13,15,16,16,18,19,19,19,
                               20,21,22,23,23,24,25,25,26,26,27,28,30,30,30,31,32,33,34,37,38,
                               43,43,44,44,46,48,51,52,55,61,65,67))
    stem(as.numeric(data2$Leaf))
    

    Output :

    The decimal point is 1 digit(s) to the right of the |
    
      0 | 11223344555566678888999
      1 | 0001222222335668999
      2 | 012334556678
      3 | 000123478
      4 | 334468
      5 | 125
      6 | 157
    

    This what you were looking for?