1
$\begingroup$

How to make a stem and leaf plot with the following data in "R":

1717 1719 1645 3739 3024 3664 3830 2991 2430 2730 3469 5086 2119 3021 3292 2844 3426 2067 3215 2767 3124 2573 2840 2449 2584 1505 1390 1645 2497 3466 3228 3192

The only way to get a reasonable amount of stems is to make the stem the first digit in each number and the leaves will all be 3 digit numbers. I am not able to split the data in this way, R represents the stem with 1 digit but round my numbers so 2584 is shown as 2| 5 (3 decimals to the right) which would be 2500. How do I adjust my stem and leaf numbers?

$\endgroup$

1 Answer 1

0
$\begingroup$

The stem function doesn't allow several digits as leaves, as far as I can tell. So you're left with implementing it from scratch. It's not too difficult but OMG it took me such a long time to get it right... I always get confused with R weird types conversions. Anyway, here it is:

d<-strtoi(unlist(strsplit('1717 1719 1645 3739 3024 3664 3830 2991 2430 2730 3469 5086 2119 3021 3292 2844 3426 2067 3215 2767 3124 2573 2840 2449 2584 1505 1390 1645 2497 3466 3228 3192',split=' ')))
dd<-ldply(d,function(x) {data.frame(stem=x %/% 1000,leaf=sprintf("%03d",x %% 1000),stringsAsFactors=FALSE)})
for (i in 0:9) { cat(paste(i,"|","")); l<-dd[dd$stem==i,]$leaf; if (length(l)>0) {cat(sort(l))}; cat("\n") }

Result:

0 | 
1 | 390 505 645 645 717 719
2 | 067 119 430 449 497 573 584 730 767 840 844 991
3 | 021 024 124 192 215 228 292 426 466 469 664 739 830
4 | 
5 | 086
6 | 
7 | 
8 | 
9 | 

Remark: there's no way to do this with the stem function simply because the purpose of a stem and leaves plot is not to display every exact value: the plot is only intended to give a "big picture" view of the distribution of the values. What matters for that is how many leaves there are for every stem, and it's enough to give a single digit since precision doesn't matter. From this point of view the stem function does the job:

stem(d)

Or variant:

stem(d,scale=0.5)
$\endgroup$
5
  • $\begingroup$ Thanks for the help! When I pasted the code it gave me 2 errors: 1)Error in ldply(d, function(x) { : could not find function "ldply" 2) 0 | Error in dd : object 'dd' not found. How do fix these errors? $\endgroup$
    – Timmy
    Commented Sep 15, 2019 at 11:28
  • $\begingroup$ I dont know why my prof would assign such a difficult problem, I am in a beginner Statistics class and this is the first time we are all using R and she didnt even teach us anything about R; she just gave us the instructions booklet $\endgroup$
    – Timmy
    Commented Sep 15, 2019 at 11:30
  • $\begingroup$ Hey, never mind, it worked. I had to install the ply package first. Thank you, your the best! I have no idea how my prof would expect us to know this $\endgroup$
    – Timmy
    Commented Sep 15, 2019 at 11:41
  • $\begingroup$ @Timmy then you're certainly not expected to come up with this answer, I did it out of curiosity but it's not standard. I added some explanations in the answer. $\endgroup$
    – Erwan
    Commented Sep 15, 2019 at 11:43
  • $\begingroup$ OK that makes sense. Thanks $\endgroup$
    – Timmy
    Commented Sep 15, 2019 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.