[英]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 chartr
在
base 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.