R中茎叶图的分组

问题描述 投票:1回答:1

我有一个茎叶图作为问题,看起来像这样:

0 | 6
1 | 179
2 | 26
3 | 2478
4 | 15699
5 | 368
6 | 24457
7 | 
8 | 56

因此,我自己创建了一个矢量,它将创建与上面相同的茎图。

data <- c(06,11,17,19,22,26,32,34,37,38,41,45,46,49,49,53,56,58,62,64,64,65,67,7,85,86)

我要做的是,我需要将词干按2分组,然后使用R绘制相应的词干图。

解决方案看起来像这样:

0-2|6*179*26
3-5|2478*15699*368
6-8|244457**56

“ *”用于分隔组中每个茎的叶子。即对于茎0-2组,它表示第一行中的叶子6对应于茎0;叶子1,7和9对应于茎1,叶子2和6对应于茎2。

我发现在stem()中没有用,所以想到了使用“间隔”函数将数据分隔为2,然后构建一个用户定义的函数,但结果是给了我相同的茎值。

是否可以通过使用内置函数/通过用户定义来获得所需的解决方案?在此先多谢!!

r dataframe plot sapply r-faq
1个回答
0
投票

这不会赢得任何选美比赛,但是您绝对可以结合使用cut和某些字符串处理来创建自己的分组stem函数。

这里是一个示例函数,已注释,因此您可以扩展它以满足您的实际需求:

grouped_stem <- function(invec, n = 3) {
  # Sequence of lowest tens and highest tens in the input data, by 10
  cuts <- seq((min(invec) %/% 10) * 10, round(max(invec), -(nchar(max(invec))-1)), 10)
  # For pretty labels in `cut`
  labs <- sub("(.*).$", "\\1", cuts)
  labs <- replace(labs, !nzchar(labs), "0")
  # List of the values according to their `cut` intervals
  temp <- split(invec, cut(invec, cuts, labs[-length(labs)], right = FALSE))
  # Only interested in the last digit
  temp <- relist(sub(".*(.)$", "\\1", unlist(temp, use.names = FALSE)), temp)
  # Paste the values together. Add in a "*" that we can get rid of later if not required
  combined <- vapply(temp, function(y) sprintf("%s*", paste(y, collapse = "")), character(1L))
  # Split by number of groups of tens per stem
  splits <- split(combined, ((seq_along(combined)-1) %/% n))
  # Construct the stems and leaves
  stems <- vapply(splits, function(x) {
    paste(names(x)[1], names(x)[length(x)], sep = " to ")
  }, character(1L))
  leaves <- vapply(splits, function(x) {
    sub("[*]$", "", paste(x, sep = "", collapse = ""))
  }, character(1L))
  # Print and store
  cat(sprintf(sprintf("%%%ss | %%s", max(nchar(stems))+2), stems, leaves), sep = "\n")
  invisible(setNames(as.list(leaves), stems))
}

运行示例数据,它会产生:

grouped_stem(data)
##   0 to 2 | 67*179*26
##   3 to 5 | 2478*15699*368
##   6 to 8 | 24457**56
© www.soinside.com 2019 - 2024. All rights reserved.