是否有人知道如何实现excel功能" offset"在r?
在excel中很容易做到这一点,但在r。
中执行此操作非常复杂我在这里附上数据和结果。我想要做的是根据data2
的年龄信息从data1
中提取人口。
例如,对于2008年,儿童在6岁时就读小学,因此result
数据框的第一行应该是2008年的6岁,但在2010年,儿童7岁就去上学,所以我需要告诉R查看2009年data2
的7岁时的价值。
另一件事是如何要求R将不同年龄的人口数加起来。在result
数据框中,对于2008年小学儿童的行数,应该将所有儿童编号从6岁到6岁不等(6 + 6),但是在年份中2010年,它应该将所有7至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)
)
答案 0 :(得分:2)
给孩子们!
as.data.frame(
mapply(function(x,y) {
c(y[x[1]-2], # first acceptable age
sum(y[(x[1]-2):(x[1]-2+x[2])]), # sum within range
y[x[1]-2+x[2]]) # last acceptable age
},
data1[-1],data2[-1])
)
# X2008 X2009 X2010 X2011
# 1 131944 135083 134071 137593
# 2 845815 867420 750306 769074
# 3 110207 113381 116538 119397