[英]How to filter results based on dependencies between two columns in one table and results from another table in R?
我想知道是否有一種方法(function 或幾行優雅的代碼)可以幫助我過濾具有這種麻煩的數據幀結構/列依賴性的結果。
我有一個場景,我的特征彼此高度相關(即表 1)。
我還有一個單獨的表,列出了每個單獨功能的另一個分數(即表 2)。
表格1:
feature1, feature2, feature_correlation_score
a, b, 0.7
c, d, 0.5
b, a, 0.7
d, c, 0.5
e, f, 0.8,
f, e, 0.8
表 2:
feature, label_correlation_score
a, 0.20
b, 0.15
c, 0.08
d, 0.04
e, 0.02
f, 0.02
我想做的是:
(1)識別每個唯一的feature1
和feature2
對(即a、b 和b、a 相同)。
(2)然后檢查表 2 中對於一對中的每個值的label_correlation_score
是多少,並且只保留每個唯一對之間具有最高label_correlation_score
的特征。
(3)將結果存儲在一個新表中,如下所示:
決賽桌:
feature, label_correlation_score
a, 0.20
c, 0.08
e, 0.02
注意:它可以是在最后一行中選擇的 e 或 f,因為它們的label_correlation_scores
是相同的。
提前致謝!
編輯:我也對使用data.table
的等效代碼感興趣。
如果您可以使用tidyverse
,這是一種方法。
feature1
小於feature2
的行,從而刪除重復項(假設兩個版本始終可用)。feature1
和feature2
label_correlation_score
分別給列后綴_1
和_2
)。label_correlation_score
列中,並將與之對應的特征存儲在feature
列中。feature
和label_correlation_score
列。library(tidyverse)
df1 <- read_csv("feature1, feature2, feature_correlation_score
a, b, 0.7
c, d, 0.5
b, a, 0.7
d, c, 0.5
e, f, 0.8,
f, e, 0.8")
df2 <- read_csv("feature, label_correlation_score
a, 0.20
b, 0.15
c, 0.08
d, 0.04
e, 0.02
f, 0.02 ")
df1 %>%
filter(feature1 < feature2) %>%
left_join(df2, by = c("feature1" = "feature")) %>%
left_join(df2, by = c("feature2" = "feature"), suffix = c("_1", "_2")) %>%
mutate(label_correlation_score = pmax(label_correlation_score_1, label_correlation_score_2),
feature = if_else(label_correlation_score_1 > label_correlation_score_2, feature1, feature2)) %>%
select(feature, label_correlation_score)
這使
# A tibble: 3 x 2
feature label_correlation_score
<chr> <dbl>
1 a 0.2
2 c 0.08
3 f 0.02
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.