how can i match values within the same dataframe?

Create a group column that has sorted origin and destination values.

df$group <- with(df,paste0(pmax(destination, origen),pmin(destination, origen)))

Once you have that you can group by group column and calculate sum of flow.

#Base R
df$totalflow <- with(df, ave(flow, group, FUN = sum))

#dplyr
library(dplyr)
df %>% group_by(group) %>% mutate(total_flow  =sum(flow))

#data.table
library(data.table)
setDT(df)[, totalflow := sum(flow), group]

This returns :

df
#  destination origen iso3_exp Importer       flow iso3_imp        gdp_o
#1         ABW    AUT       40      533    188.782      ABW 240457629696
#2         AUT    ABW       58      533    912.362      ABW 289122615296
#3         CHL    ARG       76      533   4535.649      ABW 768951320576
#4         ARG    CHL      156      533    496.458      ABW 728007573504
#5         AUT    BEL      170      533 116442.845      ABW  92507275264

#       gdp_d      distw  año  group  totalflow
#1 1320670336  8632.9708 1995 AUTABW   1101.144
#2 1320670336  7843.2555 1995 AUTABW   1101.144
#3 1320670336  4431.5712 1995 CHLARG   5032.107
#4 1320670336 14590.9239 1995 CHLARG   5032.107
#5 1320670336   929.5887 1995 BELAUT 116442.845

You remove the group column if it is not needed by df$group <- NULL.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top