字母序列到数字序列r

发布于 2025-01-31 06:30:49 字数 400 浏览 1 评论 0原文

数据框架:

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

1                           AAA
2                           AAB
3                           AAC
4                           BBA

我有一个看起来像:我想获得类似的

1                           111
2                           112
3                           113
4                           221

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

断念 2025-02-07 06:30:49

基础r中,我们可以使用chartr

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

如果替换的值具有多个字符,则一般解决方案为str_replace_all - 使用符合和替换的命名键/值向量

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

In base R, we can use 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

library(stringr)
 str_replace_all(df[[1]],   setNames(c("1", "2", "3"), c("A", "B", "C")))
[1] "111" "112" "113" "221"
我的鱼塘能养鲲 2025-02-07 06:30:49

另一个选项是使用Letters从基本R和命名向量使用将字母转换为其各自的数字。

libary(tidyverse) 

map_chr(strsplit(df$x, ""), ~ str_flatten(setNames(seq_along(LETTERS), LETTERS)[.]))
[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.

libary(tidyverse) 

map_chr(strsplit(df$x, ""), ~ str_flatten(setNames(seq_along(LETTERS), LETTERS)[.]))
[1] "111" "112" "113" "221"
一袭白衣梦中忆 2025-02-07 06:30:49

另一个选项是使用gsubfn将字母替换为数字:

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

输出:

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

Another option is using gsubfn to replace the letters with their number:

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
终遇你 2025-02-07 06:30:49

这是使用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

Here is another base R trick using utf8ToInt

> 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文