在R中同时将“1”替换为“0”和将“0”替换为“1”

Replace "1" with "0" and "0" with "1" simultaneously in R

我在 R 中有一个字符向量,例如:

> row
"1/1" "0/0" "0/0" "1/1" "0/1" "0/0" "1/0" 

我想交换“1”和“0”字符以便return一个向量:

> row2
"0/0" "1/1" "1/1" "0/0" "1/0" "1/1" "0/1"

我觉得这是一项简单的任务,但我感到很困惑,因为我假设多次调用 gsub 会在第二次调用时反转转换过程。

对于基于 R 的解决方案有什么想法吗?谢谢!

您可以使用 chartr :

row <- c("1/1", "0/0", "0/0", "1/1", "0/1", "0/0", "1/0" )
chartr('01', '10', row)
#[1] "0/0" "1/1" "1/1" "0/0" "1/0" "1/1" "0/1"