2017-04-19 81 views
1

我有一個小問題,使用dplyr group_by函數。 這樣做後:完整的列與group_by和完成

datasetALL %>% group_by(YEAR,Region) %>% summarise(count_number = n()) 

這裏是結果:

YEAR Region count_number 
<int> <int>  <int> 
1 1946  1   2 
2 1946  2   3 
3 1946  3   1 
4 1946  5   1 
5 1947  3   1 
6 1947  4   1 

我想是這樣的:

YEAR Region count_number 
<int> <int>  <int> 
1 1946  1   2 
2 1946  2   3 
3 1946  3   1 
4 1946  5   1 
5 1946  4   0 #order is no important 
6 1947  1   0 
7 1947  2   0 
8 1947  3   1 
9 1947  4   1 
10 1947  5   0 

我嘗試使用complete()從tidyr包,但它沒有成功...

+0

你能用'complete'添加你試過的代碼嗎? – aosmith

+0

請告訴我們你是如何使用'complete'的。在運行'complete'之前,你可能需要'取消組合'。另外,它取決於你在'complete'中嵌套的變量。 – eipi10

+0

這個前面的問題似乎涵蓋它... http://stackoverflow.com/questions/22523131/dplyr-summarise-equivalent-of-drop-false-to-keep-groups-with-zero-length-in –

回答

1

使用complete從t他tidyr包應該工作。你可以找到關於它的文檔here

可能發生的事是您沒有刪除分組。然後完成嘗試在每個組內添加YEARRegion的每個組合。但所有這些組合已經在分組中。因此,首先刪除分組,然後完成。

datasetALL %>% 
    group_by(YEAR,Region) %>% 
    summarise(count_number = n()) %>% 
    ungroup() %>% 
    complete(Year, Region, fill = list(count_number = 1)) 
+0

謝謝,它的工作完美 – Ben