R中的数字到字母字母功能 [英] Numeric to Alphabetic Lettering Function in R

查看:296
本文介绍了R中的数字到字母字母功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个函数,该函数对1到702的整数起作用,以一种非常特定的方式将数字转换为字母.以下是一些我希望字母功能如何工作的示例:

I have written a function which works on the integers from 1 to 702 for converting a number to a letter in a very specific way. Here are some examples of how I would like the lettering function to work:

  • 1-> A,
  • 2-> B,
  • 27-> AA,
  • 29-> AC,
  • 以此类推.

我们使用此功能对报告中的附录进行编号"/标注".我希望使其更通用,以便它可以处理任何大小的正整数.如果我可以轻松地将原始数字转换为以26为基数,这会更容易,但是在R中,我看不到一种简单的方法.

We use this function for "numbering" / "lettering" our appendices in reports. I'm looking to make it more general, such that it can handle positive integers of any size. If I could easily convert the original number to base 26, this would be easier, but I do not see an easy way to do that in R.

appendix_lettering <- function(number) {
  if (number %in% 1:26) {
    return(LETTERS[[number]])
  } else if (number %in% 27:702) {
    first_digit <- (floor((number - 1) / 26))
    second_digit <- ((number - 1) %% 26) + 1
    first_letter <- LETTERS[[first_digit]]
    second_letter <- LETTERS[[second_digit]]
    return(paste0(first_letter, second_letter))
  }
}

有人对我如何最轻松地改进此函数以处理任何正整数(或至少更多整数)有建议吗?

Does anyone have suggestions for how I can most easily improve this function to handle any positive integers (or at least many more)?

推荐答案

以下是一些替代方法:

1)编码假设b为基数.这里b =26.然后有b ^ k个带有k个字母的附录 因此,对于具有数字x的特定附录,如果n是 b + b ^ 2 + ... + b ^ n> = x的最小整数.这种不等式的LHS是几何级数,因此具有封闭形式的解决方案.用该表达式替换LHS并求解n的结果方程式,可以在下面的代码中获得n的公式.然后我们从k≤k的数中减去所有b ^ k项. n并使用此处(以及网络上的其他位置). encode执行基数转换,得到digits,这是基数base中的数字向量.最后,在每个数字上加1,然后将其用作对LETTERS的查找.

1) encode Let b be the base. Here b = 26. Then there are b^k appendices having k letters so for a particular appendix having number x it has n letters if n is the smallest integer for which b + b^2 + ... + b^n >= x. The LHS of this inequality is a geometric series and therefore has a closed form solution. Replacing the LHS with that expression and solving the resulting equation for n gives the formula for n in the code below. Then we subtract all b^k terms from number for which k < n and use the APL-like encode function found here (and elsewhere on the web). encode does the base conversion giving digits, a vector of digits in base base. Finally add 1 to each digit and use that as a lookup into LETTERS.

app2 <- function(number, base = 26) {
    n <- ceiling(log((1/(1 - base) - 1 - number) * (1 - base), base = base)) - 1
    digits <- encode(number - sum(base^seq(0, n-1)), rep(base, n))
    paste(LETTERS[digits + 1], collapse = "")
}

sapply(1:29, app2) # test

给予:

[1] "A"  "B"  "C"  "D"  "E"  "F"  "G"  "H"  "I"  "J"  "K"  "L"  "M"  "N"  "O" 
[16] "P"  "Q"  "R"  "S"  "T"  "U"  "V"  "W"  "X"  "Y"  "Z"  "AA" "AB" "AC"

另一个可以尝试的测试是:

Another test to try is:

sapply(1:60, app2, base = 3)

2)递归解决方案,这是一种递归工作的替代方法.它计算出附录编号的最后一个字母,然后将其删除,然后递归计算其左侧的部分.

2) recursive solution Here is an alternative that works recursively. It computes the last letter of the Appendix number and then removes it and recursively computes the portion to its left.

app2r <- function(number, base = 26, suffix = "") {
   number1 <- number - 1
   last_digit <- number1 %% base
   rest <- number1 %/% base
   suffix <- paste0(LETTERS[last_digit + 1], suffix)
   if (rest > 0) Recall(rest, base, suffix) else suffix
}

# tests
identical(sapply(1:29, app2r), sapply(1:29, app2))
## [1] TRUE
identical(sapply(1:60, app2r, base = 3), sapply(1:60, app2, base = 3))
## [1] TRUE

这篇关于R中的数字到字母字母功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆