简体   繁体   English

字母序列到数字序列 R

[英]Sequence of letters to sequence of numbers R

I have a data frame that looks like:我有一个看起来像这样的数据框:

df <- as.data.frame(c("AAA", "AAB", "AAC", "BBA"))
df

1                           AAA
2                           AAB
3                           AAC
4                           BBA

And I want to obtain something like:我想获得类似的东西:

1                           111
2                           112
3                           113
4                           221

In base R , we can use chartrbase R中,我们可以使用chartr

df[[1]] <- chartr("ABC", "123", df[[1]])
df[[1]]
#[1] "111" "112" "113" "221"

In case if the values that replaces have more than one character, then a general solution is str_replace_all - use a named key/value vector to match and replace如果替换的值有多个字符,则通用解决方案是str_replace_all - 使用命名键/值向量来匹配和替换

library(stringr)
 str_replace_all(df[[1]],   setNames(c("1", "2", "3"), c("A", "B", "C")))
[1] "111" "112" "113" "221"

Another option is to use LETTERS from base R and a named vector to convert the letters to their respective numbers.另一种选择是使用基数 R 中的LETTERS和命名向量将字母转换为它们各自的数字。

libary(tidyverse) 

map_chr(strsplit(df$x, ""), ~ str_flatten(setNames(seq_along(LETTERS), LETTERS)[.]))
[1] "111" "112" "113" "221"

Another option is using gsubfn to replace the letters with their number:另一种选择是使用gsubfn用它们的数字替换字母:

library(gsubfn)
v <- setNames(seq_along(LETTERS), LETTERS)
transform(df, numbers = gsubfn("(.)", as.list(v), df[[1]]))

Output:输出:

  c..AAA....AAB....AAC....BBA.. numbers
1                           AAA     111
2                           AAB     112
3                           AAC     113
4                           BBA     221

Here is another base R trick using utf8ToInt这是另一个使用utf8ToInt的基本 R 技巧

> v <- c("AAA", "AAB", "AAC", "BBA")

> sapply(v, function(x) crossprod(utf8ToInt(x) - 64, 10^((nchar(x):1) - 1)))
AAA AAB AAC BBA 
111 112 113 221

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

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