【发布时间】:2019-04-09 03:28:35
【问题描述】:
我已经阅读了现有的主题,但我所阅读的内容都与我想做的事情不符。
数据框 1:新数据(摘录)
country year sector emissions
Austria 1990 Total 6.229223e+04
Austria 1990 Regulated 3.826440e+04
Austria 1990 Unregulated 2.402783e+04
Austria 1991 Total 6.589968e+04
Austria 1991 Regulated 3.931820e+04
Austria 1991 Unregulated 2.658148e+04
数据框 2:EUETS(摘录)
country year emissions
Austria 2005 164925659
Belgium 2005 282762153
Croatia 2005 0
Cyprus 2005 16021583
Czech Republic 2005 288986144
Denmark 2005 171815416
Estonia 2005 71336242
我想做什么:
- 将
EUETS$emissions中的信息添加到新列newdata$EUETS - 此插入应基于国家和年份,并插入到
newdata$sector = "regulated"所在国家和年份的行中 -
newdata$sector = "unregulated"和newdata$sector = "Total"需要接收NA并且在任何情况下都不要接收0 - 如果
EUETS$country和/或EUETS$year中没有对应信息,则NA应插入newdata$EUETS - 如果
EUETS$emissions中有信息,但在newdata中没有匹配的年份和/或国家,则应为该信息创建一个新行,如上填写来自EUETS的值,但插入@ 987654337@ 在newdata$emissions = Total和newdata$unregulated的新单元格中。
这应该是这样的:
country year sector emissions EUETS
Austria 1990 Total 6.229223e+04 NA
Austria 1990 Regulated 3.826440e+04 2516843
Austria 1990 Unregulated 2.402783e+04 NA
Austria 1991 Total 6.589968e+04 NA
Austria 1991 Regulated 3.931820e+04 446656
Austria 1991 Unregulated 2.658148e+04 NA
Liechtenstein 2005 Total NA NA
Liechtenstein 2005 Regulated NA 654612641
Liechtenstein 2005 Unregulated NA NA
列支敦士登仅在 EUETS$country 中,在 newdata$country 中不存在,因此被添加到后一个数据框中。
这可能是几个问题/帖子合二为一,但我希望在这里提问是合适的。我自己尝试了一些东西,但没有成功,尤其是在将值填充到 newdata(国家和年份)的现有列中时。
感谢您在此任务的任何部分提供的帮助。
提前非常感谢!
北湖
【问题讨论】:
标签: r dataframe merge conditional-statements