我的一個腳本有重複的代碼段爲:函數內識別變量名
cat(capture.output(describe(semWellCases$di)),
file="./output/descriptivestats.txt",
sep="\n",append=TRUE)
cat(capture.output(describe(semWellCases$dd)),
file="./output/descriptivestats.txt",
sep="\n",append=TRUE)
cat(capture.output(describe(semWellCases$fas)),
file="./output/descriptivestats.txt",
sep="\n",append=TRUE)
本節的目標是創造一個文件,並追加統計每個變量的文件。我試圖使它成爲一個功能部分工作:
descriptiveStats <- function (vars, filename) {
for (i in vars) {
cat(capture.output(describe(i)),
file=filename,
sep="\n",append=TRUE)
}
}
我與撥打:
descriptiveStats(semWellCases[c("di","dd", "fas")], "./output/stats.txt")
的問題是輸出文件沒有變量名,他們都被列爲i
,這是我在用於循環的名稱:
1 Variables 195 Observations
--------------------------------------------------------------------------------
i
n missing distinct Info Mean Gmd .05 .10
195 0 13 0.982 5.574 2.891 2.0 3.0
.25 .50 .75 .90 .95
4.0 5.0 7.0 9.0 9.3
Value 2 3 4 5 6 7 8 9 10 11 12
Frequency 15 32 37 23 24 20 18 16 5 2 1
Proportion 0.077 0.164 0.190 0.118 0.123 0.103 0.092 0.082 0.026 0.010 0.005
Value 14 28
Frequency 1 1
Proportion 0.005 0.005
--------------------------------------------------------------------------------
在一系列附加幾個描述()輸出後,變得不可能識別摘要到相應的變量。
- 如何在每個
describe()
輸出之前打印傳遞給該函數的變量名稱?
輸出之前,他們是如何列爲'i'?是'stats.txt'有問題,因爲你不知道它來自哪裏? –
請參閱描述輸出()的第三行,那裏有'i'。我希望它是變量的名稱,無論是'dd','fas'還是'di' ... –