【发布时间】:2013-11-30 17:55:41
【问题描述】:
我有一个非常新手的问题。我正在使用 Aid Worker Security Database,该数据库记录了针对援助人员的暴力事件,以及从 1997 年至今的事件报告。事件在数据集中独立标记。我想合并给定年份在一个国家发生的所有事件,将其他变量的值相加,并为所有国家(1997-2013)创建一个具有相同年数的简单时间序列。知道怎么做吗?
df
# year country totalnationals internationalskilled
# 1 1997 Rwanda 0 3
# 2 1997 Cambodia 1 0
# 3 1997 Somalia 0 1
# 4 1997 Rwanda 1 0
# 5 1997 DR Congo 10 0
# 6 1997 Somalia 1 0
# 7 1997 Rwanda 1 0
# 8 1998 Angola 5 0
其中“df”定义为:
df <- structure(list(year = c(1997L, 1997L, 1997L, 1997L, 1997L, 1997L,
1997L, 1998L), country = c("Rwanda", "Cambodia", "Somalia", "Rwanda",
"DR Congo", "Somalia", "Rwanda", "Angola"), totalnationals = c(0L,
1L, 0L, 1L, 10L, 1L, 1L, 5L), internationalskilled = c(3L, 0L,
1L, 0L, 0L, 0L, 0L, 0L)), .Names = c("year", "country", "totalnationals",
"internationalskilled"), class = "data.frame", row.names = c(NA, -8L))
我想要这样的东西:
# year country totalnationals internationalskilled
# 1 1997 Rwanda 2 3
# 2 1997 Cambodia 1 0
# 3 1997 Somalia 1 1
# 4 1997 DR Congo 10 0
# 5 1997 Angola 0 0
# 6 1998 Rwanda 0 0
# 7 1998 Cambodia 0 0
# 8 1998 Somalia 0 0
# 9 1998 DR Congo 0 0
# 10 1998 Angola 5 0
很抱歉这个非常非常新手的问题......但到目前为止我不知道该怎么做。谢谢! :-)
【问题讨论】:
-
请阅读this,然后相应地编辑您的问题。