将矩阵与R结合

时间:2015-08-07 08:58:34

标签: r matrix

我有三个矩阵

id.df 

        column1   column2   column3   column4
 row1         1         2         3         4
 row2         5         6         7         8
 row3         9        10        11        12

names.df

        column1   column2   column3   column4
 row1         A         B         H         K
 row2         C         D         I         L
 row3         F         G         J         M

data.df

        column1   column2   column3   column4   column5  column6
 row1         1         4        8         4      5         1
 row2         5         8        7         8      3         5
 row3         4         7        5         1      7         4

我想根据行和列匹配id.dfname.df,这意味着id中的每个值都对应name.df中的名称。然后我想创建一个矩阵D,显示哪个名称对应哪个id 例如1:A 2:B 3:H 4:K 5:c 6:D 7:I 8:L ......

最后,我想用id替换名称中的data.df

输出:

将data.df中的数字替换为符合矩阵D

的名称
  column1   column2   column3   column4   column5  column6
row1   A         K         L         K       C         A
row2   C         L         I         L       H         C
row3   K         I         C         A       I         K

注意:data.df与其他两个维度不同。

1 个答案:

答案 0 :(得分:2)

如果描述中提到的数据集是matrices,那么我们可以match'data.df'和'id.df'来获取数字索引,用它来得到相应的'names.df'中的元素创建一个新的矩阵'D'。

 D <- data.df
 D[] <- names.df[match(data.df, id.df)]
 D
 #    column1 column2 column3 column4
 #row1 "A"     "K"     "L"     "K"    
 #row2 "C"     "L"     "G"     "L"    
 #row3 "F"     "G"     "K"     "M"    

更新

对于新数据集,即'data.df1',我们可以使用相同的代码

 D <- data.df1
 D[] <- names.df[match(data.df1, id.df)]
 D
 #    column1 column2 column3 column4 column5 column6
 #row1 "A"     "K"     "L"     "K"     "C"     "A"    
 #row2 "C"     "L"     "I"     "L"     "H"     "C"    
 #row3 "K"     "I"     "C"     "A"     "I"     "K"    

或没有match

的其他选项
  D[] <- setNames(c(names.df), c(id.df))[as.character(data.df1)]

我们创建了一个vectorc(names.df))并将名称设置为'id.df'(c(id.df))作为'键/值'对。 vectoras.character(data.df1))将与'key'匹配,我们会得到相应的'value'。

注意:如果数据集是“data.frame”,则应将其转换为“矩阵”以使其生效。即names.df <- as.matrix(names.df),类似于'data.df'和'id.df'。

数据

 data.df <- structure(c(1L, 5L, 9L, 4L, 8L, 10L, 8L, 10L, 4L, 4L, 8L, 12L
 ), .Dim = 3:4, .Dimnames = list(c("row1", "row2", "row3"), c("column1", 
 "column2", "column3", "column4")))

 id.df <-  structure(c(1L, 5L, 9L, 2L, 6L, 10L, 3L, 7L, 11L, 4L, 8L, 12L
 ), .Dim = 3:4, .Dimnames = list(c("row1", "row2", "row3"), c("column1", 
 "column2", "column3", "column4")))

 names.df <- structure(c("A", "C", "F", "B", "D", "G", "H", "I", "J", 
  "K", 
 "L", "M"), .Dim = 3:4, .Dimnames = list(c("row1", "row2", "row3"
 ), c("column1", "column2", "column3", "column4")))

 data.df1 <- structure(c(1L, 5L, 4L, 4L, 8L, 7L, 8L, 7L, 5L, 4L, 8L, 1L,
  5L, 
 3L, 7L, 1L, 5L, 4L), .Dim = c(3L, 6L), .Dimnames = list(c("row1", 
 "row2", "row3"), c("column1", "column2", "column3", "column4", 
 "column5", "column6")))