繁体   English   中英

取R中两个数据帧之间的差

[英]Taking the difference between two data frames in R

我一直在寻找使用R进行操作的简便方法,但找不到它,因此将其发布在此处。

假设我有以下数据框

 state1     score1     state2    score2
   A          1          A          3
   A          2          B          13
   A          1          C          5
   B          10         A          1
   B          5          B          0
   B          3          C          0
   C          2          A          5
   C          0          B          6
   C          1          C          3

第二个数据帧是

 state1     state2    score
   A          A         0
   A          B         -1
   A          C         3
   B          A         2
   B          B         1
   B          C         1
   C          A         2
   C          B         2
   C          C         1

让我们将第一个数据帧称为df1,将第二个边距称为df2。

看一下具有相同(状态1,状态2)对的df1,df2。 对于每个匹配对,从df1中的score1中减去df2中的得分,将其称为newscore1;从df2中的score2中减去df2中的分数,并将其称为newscore2。 对于这种情况,将需要以下输出。

 state1   newscore1    state2    newscore2
   A          1          A          3
   A          3          B          14
   A          -2         C          2
   B          8          A          -1
   B          4          B          -1
   B          2          C          -1
   C          0          A          3
   C          -2         B          4
   C          0          C          2

是否有一线/双线解决方案? 否则,我必须做

1)重新排序df2,以便state1,state2与df1匹配(在这种情况下,我不必做任何事情,因为df1中的第1行已经与df2中的第1行匹配,df1中的第2行已经与中的第2行匹配df2等)

2)绑定df1 $ score1-df2 $ score,df1 $ score2-df2 $ score

使用library(data.table)

进行联接(如其他解决方案所建议的),然后在一步中使用按引用更新运算符( := )添加新列。

df1[ df2, on = c("state1","state2"), `:=`(newscore1 = score1 - score, newscore2 = score2 - score)]
df1
#    state1 score1 state2 score2 newscore1 newscore2
# 1:      A      1      A      3         1         3
# 2:      A      2      B     13         3        14
# 3:      A      1      C      5        -2         2
# 4:      B     10      A      1         8        -1
# 5:      B      5      B      0         4        -1
# 6:      B      3      C      0         2        -1
# 7:      C      2      A      5         0         3
# 8:      C      0      B      6        -2         4
# 9:      C      1      C      3         0         2

只需合并两者并逐列减去:

dfm <- merge(df1, df2, by=c("state1", "state2"))
dfm$newscore1 <- dfm$score1 - dfm$score
dfm$newscore2 <- dfm$score2 - dfm$score

dfm <- dfm[c("state1", "newscore1", "state2", "newscore2")]

做到这一点最干净的方法是使用join操作。 我喜欢dplyr 例如:

state1 <- gl(3, k=3, labels=c("A", "B", "C"))
score1 <- sample(1:10, size = 9, replace = TRUE)
state2 <- gl(3, k=1, length=9, labels=c("A", "B", "C"))
score2 <- sample(1:10, size = 9, replace = TRUE)
df1 <- data.frame(state1, score1, state2, score2)

这是第一个数据框:

> df1
  state1 score1 state2 score2
1      A      3      A      6
2      A      8      B      2
3      A      3      C      6
4      B      2      A      8
5      B      3      B     10
6      B      3      C      6
7      C      7      A      2
8      C      9      B      5
9      C      6      C     10

score <- sample(-5:5, size = 9, replace = TRUE)
df2 <- data.frame(state1, state2, score)

这是第二个:

> df2
  state1 state2 score
1      A      A    -1
2      A      B     1
3      A      C    -2
4      B      A     5
5      B      B     5
6      B      C     5
7      C      A     0
8      C      B    -1
9      C      C    -3

combined_df <- df1 %>% 
  # line df1 and df2 up by state1 and state2, and combine them
  full_join(df2, by=c("state1", "state2")) %>% 
  # calculate the new columns you need
  mutate(newscore1 = score1 - score, newscore2 = score2 - score) %>% 
  # drop the extra columns
  select(state1, newscore1, state2, newscore2)

> combined_df
  state1 newscore1 state2 newscore2
1      A         4      A         7
2      A         7      B         1
3      A         5      C         8
4      B        -3      A         3
5      B        -2      B         5
6      B        -2      C         1
7      C         7      A         2
8      C        10      B         6
9      C         9      C        13

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM