比较两个字符串和不同字符的情况

发布于 2025-02-10 04:23:54 字数 256 浏览 0 评论 0原文

我正在尝试使用R.根据此比较将“主”字符串与字符串列表进行比较,我希望字符串中的所有字符都与主字符串更改为小写。

例如:主字符串为“ agg”。将字符串与[“ ATT”,“ AGT”]进行比较的列表。我想返回[“ att”,“ agt”]。订单也很重要。因此,[“ GGA”]应返回[“ GGA”]

任何帮助将不胜感激!

I'm trying to compare a "master" string to a list of strings using R. Based on this comparison, I'd like all characters in the string which differ from the master string changed to lowercase.

For example: the master string is "AGG". The list of strings being compared to is ["ATT", "AGT"]. I want to return ["Att","AGt"]. Order also matters. So ["GGA"] should return ["gGa"].

Any help would be greatly appreciated!

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

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

发布评论

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

评论(1

困倦 2025-02-17 04:23:54

您可以将所有字符首先变成小写,然后将主字符串中的这些字符变成大写。

master <- "AGG"
x <- c("ATT", "AGT", "GGA")

chartr(tolower(master), master, tolower(x))
# [1] "Att" "AGt" "GGA"

更新:如果要比较xmaster by-daracter,请尝试以下操作:

sapply(strsplit(x, ""), \(char) {
  paste(ifelse(char == strsplit(master, "")[[1]], char, tolower(char)), collapse = "")
})

# [1] "Att" "AGt" "gGa"

You could turn all characters into lowercase first, and turn those characters in the master string back to uppercase.

master <- "AGG"
x <- c("ATT", "AGT", "GGA")

chartr(tolower(master), master, tolower(x))
# [1] "Att" "AGt" "GGA"

Update: If you want to compare x and master character-by-character, try this:

sapply(strsplit(x, ""), \(char) {
  paste(ifelse(char == strsplit(master, "")[[1]], char, tolower(char)), collapse = "")
})

# [1] "Att" "AGt" "gGa"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文