Replace the numbers in the matrix with a row

I have a matrix containing integers and a data frame with multiple 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 (10,000+ rows).

I want to replace the numbers in the matrix with the corresponding V2 row in my data frame. So all entries β€œ1” end as β€œFox”, β€œ2” as β€œHorse”, etc.

  [,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 help!

+5
source share
2 answers

This seems like a 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 
+10
source

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 
+4
source

Source: https://habr.com/ru/post/1232979/


All Articles