2015-01-26 147 views
2

我想使用dplyr進行某些數據操作。背景:我有一個調查權重和一堆變量(大部分是likert-items)。我想總結每個類別的頻率和百分比,有無調查權重。在函數中使用dplyr的問題(group_by)

作爲一個例子,讓我們使用性別變量的頻率。結果應該是這樣的:

gender freq freq.weighted 
    1  292  922.2906 
    2  279  964.7551 
    9   6  21.7338 

我會爲許多變量做到這一點。所以,我決定把dplyr-code放在一個函數中,所以我只需要改變這個變量並輸入less。

#exampledata 
gender<-c("2","2","1","2","2","2","2","2","2","2","2","2","1","1","2","2","2","2","2","2","1","2","2","2","2","2","2","2","2","2") 
survey_weight<-c("2.368456","2.642901","2.926698","3.628653","3.247463","3.698195","2.776772","2.972387","2.686365","2.441820","3.494899","3.133106","3.253514","3.138839","3.430597","3.769577","3.367952","2.265350","2.686365","3.189538","3.029999","3.024567","2.972387","2.730978","4.074495","2.921552","3.769577","2.730978","3.247463","3.230097") 
test_dataframe<-data.frame(gender,survey_weight) 

#function 
weighting.function<-function(dataframe,variable){ 
    test_weighted<- dataframe %>% 
    group_by_(variable) %>% 
    summarise_(interp(freq=count(~weight)), 
       interp(freq_weighted=sum(~weight))) 
    return(test_weighted) 
} 

result_dataframe<-weighting.function(test_dataframe,"gender") 

#this second step was left out in this example: 
#mutate_(perc=interp(~freq/sum(~freq)*100),perc_weighted=interp(~freq_weighted/sum(~freq_weighted)*100)) 

這將導致以下錯誤-消息:

Error in UseMethod("group_by_") : 
    no applicable method for 'group_by_' applied to an object of class "formula" 

我已經嘗試了很多不同的東西。首先,我使用freq=n()來計算頻率,但我總是得到一個錯誤(我檢查,plyr在dplyr之前加載,而不是之後 - 它也沒有工作。)。

任何想法?我讀了關於標準評估的小插曲。但是,我總是遇到問題,不知道什麼是解決方案。

回答

12

我想你有幾個嵌套的錯誤,這會導致你的問題。最大的一個是使用count()而不是summarise()。我猜你想n()

weighting.function <- function(dataframe, variable){ 
    dataframe %>% 
    group_by_(variable) %>% 
    summarise_(
     freq = ~n(), 
     freq_weighted = ~sum(survey_weight) 
    ) 
} 

weighting.function(test_dataframe, ~gender) 

你也有過的interp()一些不必要的用途。如果您確實使用interp(),則呼叫應該看起來像freq = interp(~n()),即該名稱不在調用interp的名稱中,並且正在插補的內容以~開頭。