2015-10-05 137 views
5

我有一个矩阵包含整数和一个数据框与几列。用字符串替换矩阵中的数字

矩阵:

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

数据帧:

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" 

实际的数据帧是比这(10000+行)大得多。

我想要的是将矩阵中的数字替换为我数据框中对应的V2行。因此,所有“1”条目最终为“Fox”,“2”为“Horse”等。

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

感谢您的任何帮助!

+1

请注意,您需要一个新的矩阵 - R中的矩阵必须是单类型的(与数据框不同)。 – TARehman

回答

10

这似乎这样的伎俩:

#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 

数据:

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" ') 

输出:

[,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   
+0

@Tarehman这似乎是字符老实说:'> typeof(matrix(df2 $ V2 [mat],ncol = 6)) [1]“character”' – LyzandeR

+0

Self-moderated - I was incorrect。 :) – TARehman

+0

@Tarehman不用担心:) – LyzandeR

4

您还可以使用lookupqdapTools

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