[英]Error with merging dataframes using two columns
I have successfully merged dataframes using only one column but am having trouble merging on two. 我仅使用一列成功合并了数据框,但是在合并两列时遇到了麻烦。 I have a data set with various information on counties/states and want to merge with lat/long of that county/state from another data set.
我有一个包含有关县/州的各种信息的数据集,并希望与另一个数据集的那个县/州的纬度/经度合并。
new1 新1
Month County ST Bag1 zip
10 MIDDLESEX CT 1 06037
10 MIDDLESEX CT 1 06037
9 NEW LONDON CT NA 06355
9 NEW LONDON CT NA 06355
9 NEW LONDON CT NA 06355
9 NEW LONDON CT NA 06355
9 NEW LONDON CT NA 06355
9 NEW LONDON CT 0 06355
10 NEW LONDON CT 5 06355
10 NEW LONDON CT 3 06355
new2 新2
NAME ST FIPS Latitude Longitude
LITCHFIELD CT 9005 41.79274312 -73.24448889
HARTFORD CT 9003 41.80570047 -72.73328657
TOLLAND CT 9013 41.85298958 -72.337294
WINDHAM CT 9015 41.82754918 -71.98851586
NEW LONDON CT 9011 41.48228923 -72.10131783
FAIRFIELD CT 9001 41.27179117 -73.38473902
MIDDLESEX CT 9007 41.46673693 -72.53722401
NEW HAVEN CT 9009 41.41124892 -72.93600942
The code I am using is: 我使用的代码是:
fullDataX = merge(new, new2[ , c("NAME","ST","Latitude", "Longitude")],
by.x=c("County","NAME"),by.y=c("ST","ST"),all.x=T)
But I get the following error: 但是我收到以下错误:
Error in
[.data.frame
(CountyData, CountyData$ST == "CT", c("NAME", "ST", : undefined columns selected[.data.frame
(CountyData,CountyData $ ST ==“ CT”,c(“ NAME”,“ ST”,的错误:选择了未定义的列
I have also tried a left join in dplyr but get the following error: 我还尝试了dplyr中的左联接,但出现以下错误:
Error: cannot join on columns 'ST' x 'ST' : index out of bounds
错误:无法在列“ ST” x“ ST”上连接:索引超出范围
Any thoughts would be greatly appreciated. 任何想法将不胜感激。 I have looked at numerous other questions and answers but can't seem to find an appropriate solution.
我看过许多其他问题和答案,但似乎找不到合适的解决方案。 Thanks.
谢谢。
以下内容可能对您有帮助:
fullDataX = merge(new1, new2, by.x = c("County", "ST"), by.y = c("NAME", "ST"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.