[英]Create a data frame with matches and mismatches between two data frames
我正在嘗試創建一個熱圖,以便可視化某些預測值和預期值之間的匹配和不匹配。
如果 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))
如何創建僅包含 a & b 的公共列的第三個數據框並返回
## 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.