把A换成B,用B代替A。

时间:2022-07-30 20:06:15

I want to substitute all A's for B's and B's for A's in a string in R.

我想把所有的A代入B, B代入R中的A。

My input is

我的输入

x = "ABCDBBABDC"

and my output should be,

输出应该是,

y = "BACDAABADC"

How could I do this in one line?

我怎么能在一行里做这个呢?

I tried sub but I cannot do multiple substitutions.

我尝试了下标,但是不能做多次替换。

1 个解决方案

#1


14  

You are looking for chartr:

您正在寻找chartr:

x = "ABCDBBABDC"
chartr("AB", "BA", x)
# [1] "BACDAABADC"

#1


14  

You are looking for chartr:

您正在寻找chartr:

x = "ABCDBBABDC"
chartr("AB", "BA", x)
# [1] "BACDAABADC"