gpt4 book ai didi

r - "A"= "a"、 "B"= "b"等形式的表达式如何从 LETTERS 和字母自动构建?

转载 作者:行者123 更新时间:2023-12-05 08:47:17 24 4
gpt4 key购买 nike

我最近在读 a solutionan R4DS exercise .它包括代码:

library(stringr)
replacements <- c("A" = "a", "B" = "b", "C" = "c", "D" = "d", "E" = "e",
"F" = "f", "G" = "g", "H" = "h", "I" = "i", "J" = "j",
"K" = "k", "L" = "l", "M" = "m", "N" = "n", "O" = "o",
"P" = "p", "Q" = "q", "R" = "r", "S" = "s", "T" = "t",
"U" = "u", "V" = "v", "W" = "w", "X" = "x", "Y" = "y",
"Z" = "z")
lower_words <- str_replace_all(words, pattern = replacements)
head(lower_words)

而且我发现 replacements 向量的构造特别令人反感。没有人应该做那么多的输入,特别是考虑到 R 有向量 lettersLETTERS 内置。

有什么方法可以自动构建这样的向量吗?我尝试使用表达式对象来创建它,但我发现我们甚至无法运行像 quote("A "= "a") 因为 "A"= "a" 不是语法上有效的表达式。我还尝试使用字符串创建它,例如一些使用 paste 的技巧,但这些技巧不起作用,因为我们需要等号不是字符串。我最接近的猜测是 substitute(c(a=b), list(a=LETTERS, b=letters)),但输出显然不正确。

最佳答案

letters/LETTERS,使用

replacements <- setNames(letters, LETTERS)

关于r - "A"= "a"、 "B"= "b"等形式的表达式如何从 LETTERS 和字母自动构建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67843220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com