Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace numbers in matrix with string

Tags:

r

matrix

I have a matrix containing integers and a data frame with several columns.

Matrix:

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    4    6   1    NA   NA
[2,]    2    3   NA   NA   NA   NA
[3,]    3    4    5    6    2    1
[4,]    6    6    2    3    3   NA
[5,]    1    2    1    4    5    6
[6,]    4   NA   NA   NA   NA   NA

Data frame:

   V1   V2           V3             
1 "5P"  "Fox"       "28639"
2 "5P"  "Horse"     "33844"
3 "5P"  "Cat"       "Bes86"    
4 "5P"  "Seal"      "Bes259"   
5 "5P"  "Snake"     "Bes260"   
6 "5P"  "Platypus"  "NSA8631"   

The actual data frame is much larger than this (10000+ rows).

What I want is to replace the numbers in the matrix with the corresponding row of V2 in my data frame. So that all "1" entries end up as "Fox", "2" as "Horse" and so on.

          [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
[1,]       Fox      Seal  Platypus       Fox        NA        NA
[2,]     Horse       Cat        NA        NA        NA        NA
[3,]       Cat      Seal     Snake  Platypus     Horse       Fox
[4,]  Platypus  Platypus     Horse       Cat       Cat        NA
[5,]       Fox     Horse       Fox      Seal     Snake  Platypus
[6,]      Seal        NA        NA        NA        NA        NA

Thanks for any assistance!

like image 542
user2299015 Avatar asked Oct 05 '15 13:10

user2299015


People also ask

How do you substitute values in a matrix?

You can specify the value to replace as c , M(1,3) or M(3,1) . To replace a particular element of a matrix with a new value while keeping all other elements unchanged, use the assignment operation. For example, M(1,1) = 2 replaces only the first element of the matrix M with the value 2.

How do you replace a string in an array in Matlab?

Description. newStr = replace( str , old , new ) replaces all occurrences of the substring old with new . If old contains multiple substrings, then new either must be the same size as old , or must be a single substring.

Can matrices contain strings?

You might have heard that matrix can contain only numerical values but it is also possible to create a matrix with string values, and of course calculations using these types of matrices would not be possible.

How do you change a string to an integer in Python?

Convert a string to int : int() int() converts a string to an integer int . A string containing . or , raises an error. A comma-separated string can be converted by deleting , with replace() (replace with the empty string '' ).


2 Answers

This seems to do the trick:

#you convert the matrix to vector
#use it to index df2$V2
#and then reconstruct the matrix
matrix(df2$V2[as.vector(mat)], ncol=6)

#Or actually even better as @PierreLafortune messaged me
#you don't even need as.vector as this occurs automatically
matrix(df2$V2[mat], ncol=ncol(mat)) #result is the same

Data:

mat <- as.matrix(read.table(header=T,text='    [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    4    6   1    NA   NA
[2,]    2    3   NA   NA   NA   NA
[3,]    3    4    5    6    2    1
[4,]    6    6    2    3    3   NA
[5,]    1    2    1    4    5    6
[6,]    4   NA   NA   NA   NA   NA'))

df2 <- read.table(text='V1   V2           V3             
1 "5P"  "Fox"       "28639"
2 "5P"  "Horse"     "33844"
3 "5P"  "Cat"       "Bes86"    
4 "5P"  "Seal"      "Bes259"   
5 "5P"  "Snake"     "Bes260"   
6 "5P"  "Platypus"  "NSA8631"   ')

Output:

    [,1]       [,2]       [,3]       [,4]       [,5]    [,6]      
[1,] "Fox"      "Seal"     "Platypus" "Fox"      NA      NA        
[2,] "Horse"    "Cat"      NA         NA         NA      NA        
[3,] "Cat"      "Seal"     "Snake"    "Platypus" "Horse" "Fox"     
[4,] "Platypus" "Platypus" "Horse"    "Cat"      "Cat"   NA        
[5,] "Fox"      "Horse"    "Fox"      "Seal"     "Snake" "Platypus"
[6,] "Seal"     NA         NA         NA         NA      NA        
like image 61
LyzandeR Avatar answered Oct 30 '22 16:10

LyzandeR


You can also use lookup from qdapTools:

library(qdapTools)

matrix(lookup(c(mat), data.frame(1:nrow(df2),df2$V2)), ncol=ncol(mat))
#     [,1]       [,2]       [,3]       [,4]       [,5]    [,6]      
#[1,] "Fox"      "Seal"     "Platypus" "Fox"      NA      NA        
#[2,] "Horse"    "Cat"      NA         NA         NA      NA        
#[3,] "Cat"      "Seal"     "Snake"    "Platypus" "Horse" "Fox"     
#[4,] "Platypus" "Platypus" "Horse"    "Cat"      "Cat"   NA        
#[5,] "Fox"      "Horse"    "Fox"      "Seal"     "Snake" "Platypus"
#[6,] "Seal"     NA         NA         NA         NA      NA        
like image 44
Colonel Beauvel Avatar answered Oct 30 '22 15:10

Colonel Beauvel