Here a solution is possible data.tablethat performs a binary connection between df1and df2when calculating the length when connecting usingby = .EACHI
library(data.table)
setkey(setDT(df2), buyer)
df2[df1, list(city, count_orders = .N), by = .EACHI]
( @nicolas) ( df1 )
library(data.table)
setkey(setDT(df1), buyer)
df1[setDT(df2)[, .N, keyby = buyer], count_orders := i.N]