我有两个数据框DF1和DF2,在此我简要地报告主题为:
DF1
MONTH YEAR DISTANCE
january 2010 4.6
january 2010 3.6
february 2010 2.1
february 2010 5.3
january 2011 4.4
january 2011 5.6
february 2011 2.1
february 2011 5.3
DF2
MONTH YEAR TEMPERATURE
january 2010 0
february 2010 2
january 2011 1
february 2011 2.2
我需要在DF1中添加DF2的TEMPERATURE列,并用YEAR和MONTH之间的正确对应关系填充。
我知道如何针对每种情况进行操作,例如:
DF1$TEMPERATURE[DF1$YEAR=="2010" & DF1$MONTH=="january"]= "0"
但是以这种方式,我必须指定每个YEAR-MONTH条件(这是行不通的,我有很多对),并手动粘贴温度值。
因此,我正在寻找一种更通用的方法,该代码根据年份和月份的对应关系,用DF2的温度值填充DF1的新列TEMPERATURE。 例如:
DF1
MONTH YEAR DISTANCE TEMPERATURE
january 2010 4.6 0
january 2010 3.6 0
february 2010 2.1 2
february 2010 5.3 2
january 2011 4.4 1
january 2011 5.6 1
february 2011 2.1 2.2
february 2011 5.3 2.2
有没有办法做到这一点?
谢谢