【发布时间】:2018-06-18 22:19:37
【问题描述】:
有人知道如何在r中实现excel函数“offset”吗?
在excel中很容易做到这一点,但是在r中做到这一点就太复杂了。
我在这里附上数据和结果。我想做的是根据data1的年龄信息从data2中提取人口。
例如2008年,孩子6岁上小学,所以result数据框的第一行应该是2008年6岁的数字,但是2010年,孩子上小学7 岁上学,所以我需要告诉 R 在 2009 年查找 7 岁的值 data2。
另一件事是如何让 R 将不同年龄段的人口数相加。在result数据框中,对于“在读小学的孩子的行数”,2008年应该将6岁到(6+6)的所有孩子数相加,但在2010年应该将 7 岁到 (7+5) 岁之间的所有儿童数相加..
有人知道如何解决这个问题吗?我正在帮助孩子们,如果你能帮助我,那就太好了..
非常感谢。
data1 = data.frame(item = c("Age for primary school","Duration for primary school", "Duration for middle school", "duration for high school"),
'2008' = c(6, 6, 4, 3),
'2009' = c(6, 6, 4, 3),
'2010' = c(7, 5, 4, 3),
'2011' = c(7, 5, 4, 3))
data2 = data.frame('population by age' = seq(3, 24, by = 1),
'2008' = c(145391,
140621,
136150,
131944,
127968,
124209,
120650,
117163,
113674,
110207,
106871,
103659,
100398,
97017,
93584,
90240,
86957,
83783,
80756,
77850,
75003,
72226
),
'2009' = c(148566,
143943,
139367,
135083,
131052,
127237,
123628,
120213,
116826,
113381,
109915,
106574,
103346,
100058,
96644,
93175,
89788,
86455,
83241,
80192,
77279,
74422
),
'2010' = c(152330,
147261,
142555,
138172,
134071,
130214,
126559,
123099,
119825,
116538,
113134,
109669,
106320,
103075,
99760,
96312,
92805,
89372,
85988,
82733,
79661,
76739
),
'2011' = c(156630,
151387,
146491,
141905,
137593,
133545,
129737,
126124,
122678,
119397,
116093,
112666,
109174,
105791,
102505,
99159,
95699,
92193,
88759,
85373,
82123,
79065
))
result <- data.frame(item = c("number of children with primary school entry age",
"number of children who are in primary school",
"numer of children who are in completing primarys school"),
#"number of children who are in middle school",
#"number of children who are completing middle school",
#"number of children who are in high school",
#"number of children who are completing high school"),
"2008" = c(131944,845815,110207),
"2009" = c(135083,867420,113381),
"2010" = c(134071,750306, 116538),
"2011" = c(137593,769074,119397)
)
【问题讨论】: