gpt4 book ai didi

替换为向量

转载 作者:行者123 更新时间:2023-12-04 02:44:11 26 4
gpt4 key购买 nike

我有一个包含所有辅音的向量,我希望在给定的数据框中用“C”替换每个辅音。假设我的数据框是下面的 x:

   x <- c("abacate", "papel", "importante")

v <- c("a", "e", "i", "o", "u")
c <- c("b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z")

find <- c
replace <- "C"
found <- match(x, find)

ifelse(is.na(found), x, replace[found])

这是行不通的。谁能告诉我问题出在哪里以及如何解决?

谢谢

最佳答案

正则表达式 (gsub) 通常要灵活得多,但对于特定问题,您还可以使用运行速度更快的 chartr 函数:

old <- c("b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n",
"p", "q", "r", "s", "t", "v", "w", "x", "y", "z")
new <- rep("C", length(old))
chartr(paste(old, collapse = ""),
paste(new, collapse = ""), x)

关于替换为向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19205081/

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