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_Name | Municipality_Name |
---|---|
ANTIOQUIA | MEDELLIN |
ANTIOQUIA | MEDELLIN |
I'd like to merge the two data frames randomly, like that :
Department_Name | Municipality_Name | 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
library(dplyr)
df2<-subset(df2, select=c(Department_Name, Municipality_Name, Location,Long., Lat.))
df2 <- df2 %>% rename(Department = Department_Name, Municipality=Municipality_Name)
df1[df2, on = .(Department, Municipality, Location,Long., Lat.),
{ri <- sample(.N, 1L)
.(Department = Department[ri], Municipality = Municipality[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 !