R中的数字矩阵到数字

时间:2016-07-26 09:01:50

标签: r matrix character ascii type-conversion

我有一个4列的数据矩阵和2万多行,包括带数字,大写字母,短划线和点的条目。我想将它们表示为ASCII码,数字。我尝试了strtoi,chartoraw和utf8toint功能,但不断收到错误,主要是"要替换的项目数量不是替换长度的倍数"。

如何在没有任何NA强制的情况下将整个矩阵转换为数字矩阵?

2 个答案:

答案 0 :(得分:0)

不确定您的问题,但如果您想将字符矩阵转换为数字,请考虑使用加密摘要。

x <- matrix(letters[1:9],3,3)
x
     [,1] [,2] [,3]
[1,] "a"  "d"  "g" 
[2,] "b"  "e"  "h" 
[3,] "c"  "f"  "i"

digest(x)
[1] "b94e24be564d1af6dfedba8c3616b56d"

答案 1 :(得分:0)

> (tmp <- matrix(c(LETTERS[1:20],"3","?","a","-"),6) )
     [,1] [,2] [,3] [,4]
[1,] "A"  "G"  "M"  "S" 
[2,] "B"  "H"  "N"  "T" 
[3,] "C"  "I"  "O"  "3" 
[4,] "D"  "J"  "P"  "?" 
[5,] "E"  "K"  "Q"  "a" 
[6,] "F"  "L"  "R"  "-" 
> (tmp <-apply(tmp,c(1,2), utf8ToInt) ) 
     [,1] [,2] [,3] [,4]
[1,]   65   71   77   83
[2,]   66   72   78   84
[3,]   67   73   79   51
[4,]   68   74   80   63
[5,]   69   75   81   97
[6,]   70   76   82   45
> intToUtf8(tmp)
[1] "ABCDEFGHIJKLMNOPQRST3?a-"

根据评论进行编辑:

#Altered paste function which we will call twice
pasteC <- function(x){paste(x,collapse="")}
#Replicate your example of a cell elements being a list
values <-c(LETTERS[1:20],"3","?","a","-")
#place in a data.frame
tmp <- data.frame(id = 1:20) 
tmp$var <- list(values)
tmp$var.int <- sapply(tmp$var, function(x) pasteC(utf8ToInt(pasteC(x)) ) )
tmp