Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在函数中标识变量名_R - Fatal编程技术网

R 在函数中标识变量名

R 在函数中标识变量名,r,R,我的一个脚本有一个部分包含重复代码,如中所示: cat(capture.output(describe(semWellCases$di)), file="./output/descriptivestats.txt", sep="\n",append=TRUE) cat(capture.output(describe(semWellCases$dd)), file="./output/descrip

我的一个脚本有一个部分包含重复代码,如中所示:

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)
 }
}
问题是输出文件没有变量名,它们都列为
i
,这是我在for循环中使用的名称:

 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
--------------------------------------------------------------------------------
在一系列中附加多个descripe()输出后,无法识别相应变量的摘要

  • 在每次
    descripe()
    输出之前,如何打印传递给函数的变量名

您可以选择循环遍历data.frame的名称,并在打印
descripe

descriptiveStats <- function ( vars, filename ) {
    for (i in names(vars)) {
        cat(paste0(i, "\n"), file=filename, append=TRUE)
        cat(capture.output(describe(vars[,i])),
            file=filename, 
            sep="\n",append=TRUE)
    }
}

说明这些测试如何列为
i
?由于您不知道它来自何处,所以这是否有问题?请参阅descripe output()的第三行,
i
中有。我希望它是变量名,
dd
fas
di
…对于常规数据。frame
vars[,I]
vars[,I,drop=FALSE]
使用起来非常安全……谢谢,@RomanLuštrik。我可以问一下为什么不喜欢
'[['
吗?别误会,
[[
对data.frames(这是一种特殊的列表)非常有效,但是使用
[
会提示子集对象是data.frames(或矩阵),而不是列表本身。可能会增加可读性。请随意使用任何范例。
descriptiveStats <- function ( vars, filename ) {
    for (i in names(vars)) {
        cat(paste0(i, "\n"), file=filename, append=TRUE)
        cat(capture.output(describe(vars[,i])),
            file=filename, 
            sep="\n",append=TRUE)
    }
}