I have two dataframes df1 and df2, i want to groupby df1$delivery and create subset of df2 where one column df2$destination is equal df1$facility column and another df2$source column is not equal to same df1$facility and sum the df2$distance column
df1
facility delivery vol
ludhiana abohar 123
delhi abohar 234
mumbai abohar 345
delhi Adampu 739
jaipur Adampu 21757
lucknow Adampu 37449
df2
source destination dist
delhi ludhiana 10
mumbai ludhiana 5
ludhiana delhi 10
mumbai delhi 5
ludhiana mumbai 5
delhi mumbai 5
delhi jaipur 5
jaipur delhi 5
delhi lucknow 10
lucknow delhi 10
jaipur lucknow 5
lucknow jaipur 5
output expected
facility delivery vol pan_india_dist
ludhiana abohar 123 10
delhi abohar 234 15
mumbai abohar 345 10
delhi Adampu 739 15
jaipur Adampu 21757 10
lucknow Adampu 37449 15
x <- sapply(data4$facility,function(i)sum((mh_mh$destination[data4$facility %in% i]) & (mh_mh$source[!data4$facility %in% i]))
I had tried this formula but it is throwing not meaningful factors error