Random merging between 2 dataframes R

I have a first dataframe looking like that :

Department Municipality Location Lat. Long.
ANTIOQUIA MEDELLIN PALMITAS 6.343341 -75.69004
ANTIOQUIA MEDELLIN SANTA ELENA 6.209718 -75.50191
ANTIOQUIA MEDELLIN ALTAVISTA 6.223150 -75.62856

And a second dataframe :

Department Municipality
ANTIOQUIA MEDELLIN
ANTIOQUIA MEDELLIN

I'd like to merge the two data frames randomly, like that :

Department Municipality Location Lat Long.
ANTIOQUIA MEDELLIN SANTA ELENA 6.209718 -75.50191
ANTIOQUIA MEDELLIN PALMITAS 6.343341 -75.69004

Following this topic : Join data frames and select random row when there are multiple matches here's what i tried

df1[df2, on = .(Nombre.Departamento, Nombre.Municipio),
   {ri <- sample(.N, 1L)
   .(Departamento = Departamento[ri], Municipio = Municipio[ri])}, by = .EACHI]

Error in sample(.N, 1L) : object '.N' not found

My background in programming isn't good enough to understand the codes provided in this topic, so if someone can help with this error it'd great !



Read more here: https://stackoverflow.com/questions/68492405/random-merging-between-2-dataframes-r

Content Attribution

This content was originally published by theeconomista at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: