2013-08-26 82 views
2

我想通過對每列應用統計函數列表來總結一個非常大的數據表。 我想使用data.table作爲以前的版本plyr正在工作,但相當慢,我讀這應該快得多。 我嘗試以下,但我得到將函數列表應用到R的每一列data.table中

Error in { : 
task 1 failed - "task 1 failed - "second argument must be a list"" 

這裏是我試過

library(data.table) 
library(e1071) 
library(nortest)  

statistical_tests = list(mean, sd, kurtosis, skewness, 
         lillie.test, shapiro.test)   

summary = function(column) { 
    result = mapply(do.call, statistical_tests, column) 
    print(result) 
    return(result) 
} 

analyse_fits = function(fit_df) { 
    #get mean and standard deviation for the three parameters 
    print(fit_df) 
    setkey(fit_df, type) 
    return(fit_df[, lapply(.SD, summary), 
     by=type]) 
}         

analyse_fits(fit_df) 

例如數據fit_df功能:

constant   phase visibility type 
1: 49927.22 -2.609797e-03 0.8690605 fft 
2: 49965.89 -6.783609e-05 0.8702492 fft 
3: 50026.44 -1.109387e-03 0.8680235 fft 
4: 50063.78 2.640915e-04 0.8697564 fft 
5: 50074.89 9.999202e-04 0.8684974 fft 
6: 49964.89 -2.075373e-03 0.8708830 fft 
7: 50063.56 -9.737554e-04 0.8721360 fft 
8: 50044.11 -1.920089e-03 0.8722035 fft 
9: 50100.67 -7.487811e-04 0.8706438 fft 
10: 49962.11 4.163415e-03 0.8713016 fft 
11: 49926.63 -1.473941e-03 0.8687753 ls 
12: 49964.98 1.794244e-03 0.8710003 ls 
13: 50025.89 -1.315459e-03 0.8698475 ls 
14: 50063.40 2.891339e-04 0.8699723 ls 
15: 50074.70 1.859353e-03 0.8684841 ls 
16: 49964.43 -6.426037e-04 0.8706581 ls 
17: 50063.47 -1.646874e-03 0.8715316 ls 
18: 50043.48 -1.435637e-03 0.8713584 ls 
19: 50100.36 -2.261318e-03 0.8699203 ls 
20: 49961.76 3.659428e-03 0.8704063 ls 

我敢肯定有格式化的好方法輸出使其工作,你能幫助我嗎?

+0

正如Carl說的那樣,您似乎錯誤地調用了其中一個函數。對於'base'函數來說,無論如何這都是有效的:'DT [,c('mean','sd')),lapply(.SD,function(x)c(mean(x), sd(x)))),by = type] .' – Frank

回答

1

首先:使用traceback()找出你錯誤地調用了哪個函數。這將有助於正確格式化輸入。第二:如果您總是調用同一組統計函數,那麼只需編寫一個「包裝」函數(請參閱,例如,我的cgwtools::mystat玩具),它會依次顯式調用每個統計函數,這樣會更容易。

1

我把Frank和Carl的方法結合起來寫了一個命名的包裝函數和一個名稱列表,並添加了一個我認爲是一個整潔的想法的「stat」列。

statistical_tests = function(x) { 
    return(c(
       mean(x), 
       sd(x), 
       kurtosis(x), 
       skewness(x), 
       lillie.test(x)$p.value, 
       shapiro.test(x)$p.value)) 
} 

names = c("mean", "sd", 
     "kurtosis", "skewness", 
     "normality.p.value.lilliefors", 
     "normality.p.value.shapiro") 

analyse_fits = function(fit_df) { 
    #get mean and standard deviation for the three parameters 
    setkey(fit_df, type) 
    result = fit_df[, c(list(stat=names), lapply(.SD, statistical_tests)), by=type] 
    setkeyv(result, c("stat", "type")) 
    return(result) 
}          
+0

+1我沒有運行或測試過,只是看着它,對於速度,通常最好保持'j'儘可能簡單,並取出任何常量從它(例如'stat = names'部分),然後重塑/名稱。或者,也許只是看看'fit_df [,lapply(.SD,statistical_tests),by = type]'是多快得先看看它是否值得重新安排它。 –

相關問題