简体   繁体   English

创建一个在两个数据框之间具有匹配和不匹配的数据框

[英]Create a data frame with matches and mismatches between two data frames

I am trying to create a heatmap in order to visualize matches and mismatches between some predicted and expected values.我正在尝试创建一个热图,以便可视化某些预测值和预期值之间的匹配和不匹配。

If a is the data frame containing the predicted values and b the expected ones;如果 a 是包含预测值的数据框,而 b 是预期值;

a = rbind (sample(0:1, size=14, replace = T),sample(0:1, size=14, replace = T))
b = rbind (sample(0:1, size=40, replace = T),sample(0:1, size=40, replace = T))

How can I create a third data frame containing only the common columns of a & b and give back如何创建仅包含 a & b 的公共列的第三个数据框并返回

  • a certain value when a value is the same in the two data frames当两个数据帧中一个值相同时的某个值
  • another value if the predicted value was 0 and the expected 1如果预测值为 0 而预期值为 1,则为另一个值
  • another value if the predicted value was 1 and the expected 0.如果预测值为 1 而预期值为 0,则为另一个值。
## your example data are matrices, 
## let's make them data frames:
a = as.data.frame(a)
b = as.data.frame(b)

common_cols = intersect(names(a), names(b))

## see where they are equal
## TRUE means equal, FALSE means not equal
a[common_cols] == b[common_cols]
#         V1   V2    V3   V4    V5    V6   V7    V8    V9   V10   V11   V12   V13  V14
# [1,]  TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE TRUE
# [2,] FALSE TRUE  TRUE TRUE  TRUE FALSE TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE TRUE

## see the difference
## 0 means a and b are equal
## 1 means a is 1 and b is 0
## -1 means a is 0 and b is 1
a[common_cols] - b[common_cols]
#   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14
# 1  0  0 -1  0  1 -1  0  1  0  -1   1   0  -1   0
# 2  1  0  0  0  0  1  0  0 -1   0  -1  -1   0   0

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

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