merge - Inner Join with conditions in R -


i want inner join condition 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 

the result want

  term  sec value    t1   s1   30    t2   s2   20    t3   s3   10 

basically joining 2 tables , column value taking

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

i have struggled not found way conditional merge in base r. if not possible base r, dplyr should able inner_join() not aware of package.

so, suggestion base r and/or dplyr appreciated

editing

i have included original data asked. data here

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

df1 first table , df2 second. df2 starts 168th row.

all logic same , want join these 2 tables length 160 rows each. want join id , take difference of column value both tables. resultant dataset should have same number of rows 160 column diff

here "base r" solution using merge() function on term column shared original df1 , 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 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -