根据另一个数据框中的值向数据框中添加列

时间:2020-05-20 12:55:04

标签: r dataframe

我有2个数据帧:

dat1 <- data.frame(Loc = rep(c("NY","MA","FL","GA"), each = 10),
                   ID = rep(c(1:8), each=5),
                   var1 = rnorm(40),
                   var2=rnorm(40))
dat2 <- data.frame(Loc = c("NY","MA","FL","GA"),
                   Region = c("a","b","c","d"),
                   Region2 = c(c("Pi","La","MM","TT")))

什么是有效的方法: 如果{中的Region是{ {1}}等于Region2中的dat1,使LocIDLoc的值等于{{ 1}}

所以dat1现在看起来像这样:

Loc

1 个答案:

答案 0 :(得分:0)

您可以在dat1中创建变量Region和Region2并按以下顺序重新排列列:

dat1[c("Region","Region2")] <-  dat2[match(dat1$Loc,dat2$Loc),][c("Region","Region2")]
dat1 <- dat1[c("Loc", "Region", "Region2", "ID", "var1", "var2")]

结果:

> dat1
   Loc Region Region2 ID        var1        var2
1   NY      a      Pi  1  1.57302518  0.96922316
2   NY      a      Pi  1  0.01993612  0.90536433
3   NY      a      Pi  1  1.85823878 -0.03686603
4   NY      a      Pi  1 -0.35410296  0.02008605
5   NY      a      Pi  1  0.29076713  0.14388346
6   NY      a      Pi  2  0.07292062 -0.02179933
7   NY      a      Pi  2  0.54502273 -0.04746912
8   NY      a      Pi  2 -0.05769239 -0.33451639
9   NY      a      Pi  2 -1.46205312 -2.04213091
10  NY      a      Pi  2 -1.44839608 -0.69481274
11  MA      b      La  3 -0.75283657  0.72925342
12  MA      b      La  3  0.87019570 -0.64529074
13  MA      b      La  3 -0.56062361  0.94981025
14  MA      b      La  3 -0.87635324 -0.09053592
15  MA      b      La  3 -1.69284597 -0.14656359
16  MA      b      La  4  1.18999938 -0.81754130
17  MA      b      La  4  0.55118437  0.65882893
18  MA      b      La  4 -1.11357631  0.27600818
19  MA      b      La  4  0.95069038  0.31415471
20  MA      b      La  4  0.76774620  0.03045411
21  FL      c      MM  5 -0.28967077 -1.08580613
22  FL      c      MM  5 -0.79998319  1.00462730
23  FL      c      MM  5 -1.97087202 -0.06778605
24  FL      c      MM  5  1.80017245  0.17001177
25  FL      c      MM  5 -0.07892071 -1.16539223
26  FL      c      MM  6 -0.04199795 -0.48489442
27  FL      c      MM  6 -0.04976197 -1.52334960
28  FL      c      MM  6  0.88848477 -0.33826329
29  FL      c      MM  6  0.96300053  0.84447027
30  FL      c      MM  6  0.63643389  1.62348681
31  GA      d      TT  7 -0.15093406  1.53619256
32  GA      d      TT  7 -0.20502491  0.50564448
33  GA      d      TT  7 -0.33412740  0.63807512
34  GA      d      TT  7 -0.88849709  0.37752803
35  GA      d      TT  7  0.62979577  1.26866752
36  GA      d      TT  8  0.15635732 -0.45484752
37  GA      d      TT  8  0.27774003 -1.12692901
38  GA      d      TT  8 -0.33433302  1.10205130
39  GA      d      TT  8 -0.22294978  1.04998443
40  GA      d      TT  8 -0.94599670  1.05748818