简体   繁体   English

如何在R中获得5位数字的茎叶图?

[英]How to get a stem and leaf plot for 5 digit numbers in R?

I have been trying to solve this but haven't succeeded.我一直试图解决这个问题,但没有成功。

I have to plot the following stem and leaf plot in R-我必须在 R- 中绘制以下茎叶图

22|372
23|512, 688, 941
24|706
25|020, 057, 128, 400, 446, 575, 579
26|183, 894, 982
27|671, 711, 744
28|240, 280, 551, 731, 821, 936
29|141, 405, 567, 596, 771, 923
30|001, 180, 296, 578
31|319, 727
32|151, 677, 779, 922, 996
33|276, 404
34|071, 334
36|043, 298
39|244, 453
42|120, 706

This is the code I have used-这是我使用的代码-

income<- c(30941,25128,32151,26183,23512,32996,33276,42706,32779,42120,29596,28821,30001,25057,33404,28240,28280,29141,25579,25446,27744,36298,39244,30296,34071,22372,28936,25020,29771,30180,34334,39543,23941,36043,27711,26962,29406,25575,28731,31727,31319,25400,26894,27671,28551,24306,29567,32922,32677,23688,29923,30578)

View(income)
stem(income,scale=2, width=100)

The output I am getting is-我得到的输出是-

The decimal point is 3 digit(s) to the right of the |

  22 | 4579
  24 | 30114466
  26 | 290777
  28 | 236789146689
  30 | 0236937
  32 | 2789034
  34 | 13
  36 | 03
  38 | 25
  40 | 
  42 | 17
d = sort(income)
n = 3
d2 = sapply(split(d, trunc(d/10^n)), function(x){
    before = trunc(x[1]/10^n)
    fmt = paste0("%0", n, "d")
    after = toString(sprintf(fmt, sort(x %% 10^n)))
    paste(before, after, sep = " | ")
})

for(x in d2){
    cat(x)
    cat("\n")
}

#22 | 372
#23 | 512, 688, 941
#24 | 306
#25 | 020, 057, 128, 400, 446, 575, 579
#26 | 183, 894, 962
#27 | 671, 711, 744
#28 | 240, 280, 551, 731, 821, 936
#29 | 141, 406, 567, 596, 771, 923
#30 | 001, 180, 296, 578, 941
#31 | 319, 727
#32 | 151, 677, 779, 922, 996
#33 | 276, 404
#34 | 071, 334
#36 | 043, 298
#39 | 244, 543
#42 | 120, 706

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM