-2

I have a data matrix of 4 columns and more than 20 thousand rows consisting of entries with numbers, capital letters, dashes, and dots. I want to represent them as their ASCII code, numbers. I tried strtoi, chartoraw, and utf8toint functions but received errors constantly, mainly "number of items to replace is not a multiple of replacement length".

How can I convert the whole matrix to a numeric matrix without any NA coercion?

  • 3
    Give a `dput(head(your_matrix)` as I can't get how you can have different classes in a matrix. – Tensibai Jul 26 '16 at 09:05
  • 3
    Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Jul 26 '16 at 09:05

2 Answers2

0

Not sure your question, but if you want to convert matrices of characters to numeric, consider a cryptographic digest.

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"
shayaa
  • 2,787
  • 13
  • 19
  • Hello, thank you for your answer ! Now, I will try to combine the numbers in the list, as one number. For example, c(65, 89, 65, 69, 73, 65, 73, 65, 73, 66, 46) is one of the elements in the matrix. I want to represent it as 658965697365736646 and interpret it as an integer. Is that possible? – Burçak Saraç Jul 26 '16 at 11:40
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-"

Edit as per comment:

#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  
Philip Parker
  • 639
  • 4
  • 8
  • Hello, thank you for your answer it really helped! Now, I will try to connect the number vector, as one number. For example, c(65, 89, 65, 69, 73, 65, 73, 65, 73, 66, 46) is one of the elements in the matrix. I want to represent it as 658965697365736646 and interpret it as an integer. Is that possible? – Burçak Saraç Jul 26 '16 at 11:02