2016-11-04 55 views
-2
head(per1) 
     flowcorrelativeid nrodocumento grupo_fict cod_jer 
21   431167  49574917   3  1146 
49   546385  54692750   1  320 
50   546388  48552750   2  320 
54   454662  54731807   8  104 
58   484984  17549854   4  104 
78   360883  46867674   1  101 
79   360883  56864674   7  101 
80   360883  46867668   1  258 
81   360883  66847668   5  258 
81   360883  57364201   1  178 
88   360883  58364201   6  178 

我有這樣的數據幀,並且我想產生大量這樣的該數據幀的子集的:生成的數據幀與循環řsusbet

data_frame_104_1=subset(per1,per1$cod_jer==104 & per1$grupo_fict=1) 
data_frame_104_2=subset(per1,per1$cod_jer==104 & per1$grupo_fict=2) 
. 
. 
. 
data_frame_104_8=subset(per1,per1$cod_jer==104 & per1$grupo_fict=8) 

然後

data_frame_101_1=subset(per1,per1$cod_jer==101 & per1$grupo_fict=1) 
data_frame_101_2=subset(per1,per1$cod_jer==101 & per1$grupo_fict=2) 
. 
. 
. 
data_frame_101_8=subset(per1,per1$cod_jer==101 & per1$grupo_fict=8) 

我需要這對每個 「cod_jer」(52組)和

我該怎麼做每一個 「grupo_fict」(8組)?

有什麼建議嗎?

謝謝!

+1

只需使用'split'即'分裂(PER1,列表(PER1 $ cod_jer,PER1 $ grupo_fict),滴= TRUE)',並獲得'data.frames'一個'list' BTW,如果您需要根據這兩列進行彙總,則可以使用「group_by」操作不使用任何「split」來執行此操作 – akrun

回答

0

如果你想使用循環,你可以嘗試一下。像這樣:

grupo_fict_groups = as.numeric(names(table(per1$grupo_fict))) 
cod_jer_groups = as.numeric(names(table(per1$cod_jer))) 

data.frame = data.frame(NA) 

for(i in 1:length(cod_jer_groups)){ 
    for(j in 1:length(grupo_fict_groups)){ 
    data.frame = subset(per1,per1$cod_jer==cod_jer_groups[i] & per1$grupo_fict==grupo_fict_groups[j]) 
    assign_string = paste("data_frame",cod_jer_groups[i],grupo_fict_groups[j],sep = "_") 
    assign(assign_string,data.frame) 
    } 
} 
+0

謝謝! Secondone將是完美的,但我不能使它的工作:( – Natuk

+0

我編輯的代碼現在它應該工作... – burton030

+0

現在的作品!!!!!完美!!!謝謝你這麼多! – Natuk