2016-11-10 62 views
-2

我詢問了一個關於data.table的前一個問題(r - data.table - Why is the data.table result 1 numeric value when it should be rounded to 3 decimal places?),並顯示了一個數字結果。意見建議我只使用by = cyl,但這不會給我dplyr結果。因此,我在這裏問一個新問題。r - data.table - 按四捨五入將多個data.table結果組成

如何獲得與data.table相同的結果(請參見下面的dplyr代碼)?

# using dplyr 
mtcars1 %>% 
group_by(cyl) %>% 
select(disp) %>% 
mutate(displace = round(disp/sum(disp), digits = 3)) 

# Adding missing grouping variables: `cyl` 
# Source: local data frame [32 x 3] 
# Groups: cyl [3] 
# 
#  cyl disp displace 
# <dbl> <dbl> <dbl> 
# 1  4 108.0 0.093 
# 2  4 146.7 0.127 
# 3  4 140.8 0.122 
# 4  4 78.7 0.068 
# 5  4 75.7 0.065 
# 6  4 71.1 0.061 
# 7  4 120.1 0.104 
# 8  4 79.0 0.068 
# 9  4 120.3 0.104 
# 10  4 95.1 0.082 
# # ... with 22 more rows 

我曾經嘗試這樣做(見上面提到的以前的帖子):

# Group cars by number of cylinders and the computed share of displacement 
# using data.table 
setkey(mtcars2, "cyl") 
mtcars2[ , .(displace = round(disp/sum(disp), digits = 3)), by = list(cyl, disp)] 

# cyl disp displace 
# 1: 4 108.0  1 
# 2: 4 146.7  1 
# 3: 4 140.8  1 
# 4: 4 78.7  1 
# 5: 4 75.7  1 
# 6: 4 71.1  1 
# 7: 4 120.1  1 
# 8: 4 79.0  1 
# 9: 4 120.3  1 
# 10: 4 95.1  1 
#  cyl disp displace 

這並不在這裏工作(雖然它的工作:How to group data.table by multiple columns?

mtcars2[ , displace = round(disp/sum(disp), digits = 3), by = list(cyl, disp)] 

# Error in `[.data.table`(mtcars2, , displace = round(disp/sum(disp), digits = 3), : 
# unused argument (displace = round(disp/sum(disp), digits = 3)) 

這沒有按」 t提供我想要的所有列(如r - data.table - Why is the data.table result 1 numeric value when it should be rounded to 3 decimal places?中所建議的):

mtcars2[ , .(displace = round(disp/sum(disp), digits = 3)), by = cyl] 

謝謝。

+4

'mtcars2 [(取代= round(disp/sum(disp),digits = 3),disp),by = cyl]'如果要將結果保留在'j'位置,可以將列'disp'包裝到列表中。 – Psidom

+0

@Psidom你能評論一個答案嗎?謝謝。它非常完美! – iembry

回答

3

data.table使用summary語法,即不使用:=,您可以通過在該位置j將在列表中列在您的結果列:

mtcars2[,.(displace = round(disp/sum(disp), digits = 3), disp), by = cyl] 

# cyl displace disp 
# 1: 6 0.125 160.0 
# 2: 6 0.125 160.0 
# 3: 6 0.201 258.0 
# 4: 6 0.175 225.0 
# 5: 6 0.131 167.6 
# 6: 6 0.131 167.6 
# 7: 6 0.113 145.0 
# ... 
+0

@Psidom,謝謝你的解釋和答案。有用。 – iembry

+0

@thelatemail謝謝您的建議,但我喜歡我之前的回答。 – iembry