内在联合条件在R [英] Inner Join with conditions in R

查看:82
本文介绍了内在联合条件在R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做内部连接,条件是它应该减去2列。

I want to do inner join with the condition that it should give me subtraction of 2 columns.

df1 = data.frame(Term = c("T1","T2","T3"), Sec = c("s1","s2","s3"), Value =c(10,30,30))

df2 = data.frame(Term = c("T1","T2","T3"), Sec = c("s1","s3","s2"), Value =c(40,20,10)

 df1
 Term Sec Value
  T1  s1    10
  T2  s2    30
  T3  s3    30

  df2
  Term  Sec Value
  T1  s1    40
  T2  s3    20
  T3  s2    10

我想要的结果是

  Term  Sec Value
   T1   s1   30
   T2   s2   20
   T3   s3   10

基本上我正在加入两个表格,而我正在使用的列值

Basically I am joining two tables and for the column value I am taking

Value=  abs(df1$Value - df2$Value)

我一直在努力但是在基础R中找不到任何方式来做这个条件合并。可能如果不可能使用base R,dplyr应该能够使用inner_join()来做到这一点,但是我不太清楚这一点

I have struggled but could not found any way to do this conditional merge in base R. Probably if it is not possible with base R, dplyr should able to do that with inner_join() but I am not well aware with much of this package.

所以,任何有R和/或dplyr的建议将不胜感激。

So, any suggestion with base R and/or dplyr will be appreciated

EDITING

我已经按要求列出了我的原始数据。我的数据在这里

I have included my original data as asked. My data is here

https://jsfiddle.net / 6z6smk80 / 1 /

DF1是第一个表,DF2是第二个。 DF2从第168行开始。

DF1 is first table and DF2 is second. DF2 starts from 168th row.

所有逻辑相同,我想加入这两个长度为160行的两个表。我想通过ID加入,并从两个表中获取列值。所得到的数据集应该具有相同数量的行,160是额外的列diff

All logic same , I want to join these two tables whose length is 160 rows each. I want to join by ID and take difference of column Value from both tables. The resultant dataset should have same number of rows which is 160 with extra column diff

推荐答案

这里是一个base R解决方案原始 df1 条款中的 merge() $ c>和 df2 数据框:

Here is a "base R" solution using the merge() function on the Term column shared by your original df1 and df2 data frames:

df_merged <- merge(df1, df2, by="Sec")
df_merged$Value <- abs(df_merged$Value.x - df_merged$Value.y)
df_merged <- df_merged[, c("Sec", "Term.x", "Value")]
names(df_merged)[2] <- "Term"

> df_merged
  Sec Term Value
1  s1   T1    30
2  s2   T2    20
3  s3   T3    10

这篇关于内在联合条件在R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆