Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何翻译字符串中的所有字符_R_String - Fatal编程技术网

R 如何翻译字符串中的所有字符

R 如何翻译字符串中的所有字符,r,string,R,String,我正在R中构建一个函数,函数将以字符串(如“ABCDEFG”)和矩阵作为输入,并根据矩阵将字符串中的每个字符转换为另一个字符串 我尝试的代码是: plugboard <- function() { matrix(sample(letters, 26, replace = FALSE, prob = NULL), nrow = 2, ncol = 13) } 它应该将“wfupksal”变成“ksalwfup” 我的意思

我正在R中构建一个函数,函数将以字符串(如“ABCDEFG”)和矩阵作为输入,并根据矩阵将字符串中的每个字符转换为另一个字符串

我尝试的代码是:

plugboard <- function() {
    matrix(sample(letters, 26, 
                  replace = FALSE, prob = NULL), 
           nrow = 2, ncol = 13)
}
它应该将“wfupksal”变成“ksalwfup”

我的意思是,代码应该将第1行中的所有字符转换为第2行中的字符,并将字符串中的vica转换为第2行中的字符。但我只能用一种方法(将第2行中的所有字符更改为第1行中的字符)

示例:如果我们以上面的矩阵“x”为例,字符串“你好”应转换为“cib crk uoi bra”。在这种情况下,字符串中第一行中的所有字符都将更改为第二行中的字符,反之亦然


R中是否有反转字符串中字符的函数?

可能不清楚条件

v1 <- apply(x, 1, paste, collapse="")
chartr(paste(v1, collapse=""), paste(rev(v1), collapse=""), m)
#[1] "ksalwfup"

v1@akrun的解决方案更加优雅,为我引入了一个新的功能
chartr
,但这里有一个更冗长的方法和演练:

mat <- matrix(letters[1:6], nrow = 2, byrow = TRUE)
mat
#     [,1] [,2] [,3]
# [1,] "a"  "b"  "c" 
# [2,] "d"  "e"  "f" 

inputs <- mat[1, ]
outputs <- mat[2, ]

# See how this gives you 1 3
match(c("a", "c"), inputs)
# Then...
outputs[match(c("a", "c"), inputs)]

# So...
paste(outputs[match(unlist(strsplit("ac", "")), inputs)], collapse = "")

# In a function:
decode <- function(matix, string) {
  inputs <- mat[1, ]
  outputs <- mat[2, ]

  paste(outputs[match(unlist(strsplit(string, "")), inputs)], collapse = "")

}

decode(matix, "ac")
# [1] "df"

mat首先,我将创建一个mat并定义一条消息:

set.seed(1)
mat <- plugboard()
message <- "isbba"
set.seed(1)

mat游戏进行得很晚,但有一个功能可以容纳消息中的空格:

set.seed(42)

x <- plugboard()

message <- "example text"

codedecode <- function(message_, matrix) {
  output <- ""
  newmat <- matrix[nrow(matrix):1, ]
  splitmessage <- unlist(strsplit(message_, ""))
  for (i in splitmessage) {
    nl = newmat[which(matrix == i)]
    output <-
      paste(output, ifelse(length(nl != 0), nl, " "), sep = "")
  }
  return(output)
}

> scrambled <- codedecode(message, x); print(scrambled)
[1] "izqpmri gizg"
> unscrambled <- codedecode(scrambled, x); print(unscrambled)
[1] "example text"
set.seed(42)

x字符串是手动键入的任何字符串。在本例中:“m=”wfupksal“。该字符串没有意义,只是用于测试。我尝试模拟一个小型解密机。如果我们以上面的矩阵“x”为例,字符串“hey how are you”应该转换为“cib crk uoi bra”“在这种情况下,字符串中位于中的所有字符都将更改为第二行中的字符,反之亦然。”。
set.seed(1)
mat <- plugboard()
message <- "isbba"
decode_letter <- function(mat, letter){
  pos <- which(letter == mat, arr.ind = T)
  letter <- mat[ifelse(pos[1] == 1, 2, 1), pos[2]]

  return(letter)
}
message_split <- unlist(strsplit(x = message, split = ""))

letter_dec <- sapply(message_split, FUN = decode_letter, mat = mat, simplify = T)
message_dec <- paste(letter_dec, collapse = "")

message_dec
[1] "hello"
set.seed(42)

x <- plugboard()

message <- "example text"

codedecode <- function(message_, matrix) {
  output <- ""
  newmat <- matrix[nrow(matrix):1, ]
  splitmessage <- unlist(strsplit(message_, ""))
  for (i in splitmessage) {
    nl = newmat[which(matrix == i)]
    output <-
      paste(output, ifelse(length(nl != 0), nl, " "), sep = "")
  }
  return(output)
}

> scrambled <- codedecode(message, x); print(scrambled)
[1] "izqpmri gizg"
> unscrambled <- codedecode(scrambled, x); print(unscrambled)
[1] "example text"