0

I'm a newbe in R and have the following question:

I have two datasets (df1 and df2) that slightly differ from each other.

df1=(city=c("A", "B", "C"),Niscode = 100:110, province=c("NA", "Ant"))
df2=(city=c("A", "B", "C"), province=c("NA", "Ant"), Lokstat=1:5)

Now I want to merge them based on the following criteria because the same city can appear in differnt provinces:

If the name of the city in df1 and df2 and the province name in df1 and df2 are the same than add the Lokstat from df2 in df1.

The problem is, that in df1 the city name occurs more than once. So if I tried df= merge(dataset,dataset2, by.x = c("city", "Prov"), by.y = c("city", "Prov")) I got Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column

I guess I have to programm loops but I'm not sure how.

Thanks for your help!

Zoe2300
  • 1
  • 1
  • You should look at `?merge()`, but this should do the trick `merge(df1, df2, by.x = c("city", "Niscode"), by.y = c("city", "province"))` – simone May 31 '17 at 13:28
  • why not just `merge(df1, df2, by = c("city", "province"))` ? – moodymudskipper May 31 '17 at 13:32
  • I already tried `merge(df1, df2, by.x = c("city", "Niscode"), by.y = c("city", "province"))` but I got `Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column`. In on of the table the name of the city occurs more than once because the city=birth place of the individuals. And some of them are born at the same place. – Zoe2300 May 31 '17 at 14:49

0 Answers0