如何用第二个表R中的值替换变量?

时间:2016-07-20 06:30:04

标签: r dataframe

我有一张这样的表

Timeline1 Timeline2 Timeline3 
  v3       v7       v11
  v4       v1       v12
  v6       v8       v13
  v2       v9       v17  
  v5       v10      v16

df1<- structure(list(Timeline1 = structure(c(2L, 3L, 5L, 1L, 4L), .Label = c("v2", 
"v3", "v4", "v5", "v6"), class = "factor"), Timeline2 = structure(c(3L, 
1L, 4L, 5L, 2L), .Label = c("v1", "v10", "v7", "v8", "v9"), class = "factor"), 
    Timeline3 = structure(c(1L, 2L, 3L, 5L, 4L), .Label = c("v11", 
    "v12", "v13", "v16", "v17"), class = "factor")), .Names = c("Timeline1", 
"Timeline2", "Timeline3"), class = "data.frame", row.names = c(NA, 
-5L))

我有一张清单表

Name    Meaning
v1         C
v2         D
v3         R
v4         T
v5         Y
v6         W
v7         Q
v8         A
v9         Z
v10        X
v11        V
v12        B
v13        N
v14        J
v15        L
v16        O
v17        P
V18        U
V19        F

数据框可以在下面找到

df2 <- structure(list(Name = structure(c(1L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .Label = c("v1", 
"v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "V18", 
"V19", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9"), class = "factor"), 
    Meaning = structure(c(3L, 4L, 12L, 13L, 18L, 16L, 11L, 1L, 
    19L, 17L, 15L, 2L, 8L, 6L, 7L, 9L, 10L, 14L, 5L), .Label = c("A", 
    "B", "C", "D", "F", "J", "L", "N", "O", "P", "Q", "R", "T", 
    "U", "V", "W", "X", "Y", "Z"), class = "factor")), .Names = c("Name", 
"Meaning"), class = "data.frame", row.names = c(NA, -19L))

我想使用清单中显示的值替换第一个表,最终输出可能是这样的

Timeline1 Timeline2 Timeline3 
  R         Q          V
  T         C          B
  W         A          N
  D         Z          P  
  Y         X          O

2 个答案:

答案 0 :(得分:3)

我们可以使用match。使用&#39; Name&#39;将第一个数据集转换为matrixmatch。 &#39; df2&#39;的列获取数字索引,并在此基础上我们得到相应的元素&#39;含义&#39;并将输出分配给&#39; df1&#39;

df1[] <- df2$Meaning[match(as.matrix(df1), df2$Name)]
df1
#  Timeline1 Timeline2 Timeline3
#1         R         Q         V
#2         T         C         B
#3         W         A         N
#4         D         Z         P
#5         Y         X         O

或者,我们可以使用mutate_each中的dplyr match我们library(dplyr) df1 %>% mutate_each(funs(df2$Meaning[match(., df2$Name)])) # Timeline1 Timeline2 Timeline3 #1 R Q V #2 T C B #3 W A N #4 D Z P #5 Y X O 每个列都带有&#39;名称&#39;并将其替换为相应的&#39;含义&#39;元件。

nextDouble()

答案 1 :(得分:1)

以基本R方式执行此操作

 df <- data.frame(Timeline1 = c("v1","v2","v4"),Timeline2 = c("v3","v6","v5"))
 ref <- data.frame(Var = paste("v",1:6,sep=""), Val = LETTERS[21:26])

 df
 #   Timeline1 Timeline2
 # 1        v1        v3
 # 2        v2        v6
 # 3        v4        v5

 ref
 #   Var Val
 # 1  v1   U
 # 2  v2   V
 # 3  v3   W
 # 4  v4   X
 # 5  v5   Y
 # 6  v6   Z

 df$Timeline1 <- ref$Val[match(df$Timeline1,ref$Var)]
 df$Timeline2 <- ref$Val[match(df$Timeline2,ref$Var)]

 df
 #   Timeline1 Timeline2
 # 1         U         W
 # 2         V         Z
 # 3         X         Y