字母序列到数字序列r
数据框架:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在
基础r
中,我们可以使用chartr
,如果替换的值具有多个字符,则一般解决方案为
str_replace_all
- 使用符合和替换的命名键/值向量In
base R
, we can usechartr
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另一个选项是使用
Letters
从基本R和命名向量使用将字母转换为其各自的数字。Another option is to use
LETTERS
from base R and a named vector to convert the letters to their respective numbers.另一个选项是使用
gsubfn
将字母替换为数字:输出:
Another option is using
gsubfn
to replace the letters with their number:Output:
这是使用
utf8toint
的另一个基本r技巧Here is another base R trick using
utf8ToInt