我知道現在已經有很多關於「求和」的問題,但是,我沒有解決我的問題。下面是它:面板數據 - 按組進行求和並創建新變量
DF1是我的簡化的數據集
> df1 = data.table(Year = c(2009,2009,2009,2009,2009,2009,2009,2009,2010,2010,2010,2010),
ID = c(1621, 1621, 1628,1628,3101, 3101,3105,3105,1621, 1621, 1628,1628),
category= c("0910","0910","0911","0913", "0914", "0910","0910","0911","1014","1012","1011","1013"),
var1 = c(60,70, 400,300,15,20, 200,150,61,71,401,301))
DF2是期望的結果(見VAR2):
> df2 = data.table(Year = c(2009,2009,2009,2009,2009,2009,2009,2009,2010,2010,2010,2010),
ID = c(1621, 1621, 1628,1628,3101, 3101,3105,3105,1621, 1621, 1628,1628),
category= c("0910","0910","0911","0913", "0914", "0910","0910","0911","1014","1012","1011","1013"),
var1 = c(60,70, 400,300,15,20, 200,150,61,71,401,301),
var2= c(130,130,700,700,35,35,350,350,132,132,702,702))
所以我想計算的var1
通過分組的款項ID
和前兩個整數category
因此,如果變量類別的前兩個整數是09(或10個d等),然後根據組ID
和前兩個整數category
分配到var2
的總和。然後,同一類別中的相同ID應分配相同的總和。
我試圖通過
> df1$var2 = rep(NA, rep(length(df1$ID)))
df1$var2 = ifelse(substr(df1$category,1,2)=="09", by(df1[Year==2009,]$var1, df1[Year==2009,]$ID,sum), df1$var2)
df1$Var2 = ifelse(substr(df1$category,1,2)=="10", by(df1[Year==2010,]$var1, df1[Year==2010,]$ID,sum), df1$var1)
但這裏的款項未分配到正確的項目achiev這一點。
有人能幫我嗎?
請花些時間格式化您的代碼。 – lmo
你可以通過突出顯示你的代碼和Ctrl + K來做到這一點 – useR