时间:2015-09-13 10:19:40

标签: r

我正在写一篇论文,我想提高自己的工作效率。现在,我运行了几个函数,例如单位根测试或相关性测试,并将 R 控制台输出传输到表中;手动。有没有办法将函数的结果输出到表中?

我有* .csv表中的以下数据

col1 col2 col3 col4 col5 col6
x    x    x    x    x    x
x    x    x    x    x    x
x    x    x    x    x    x
x    x    x    x    x    x
  1. 是否可以同时将adf.test(table$col1)kpss.test(table$col1)pp.test(table$col1)应用于所有列并将结果输出到表中?

  2. 是否可以对cor.test(table$col1, table$col2, type="pearson")等相关结果执行相同操作?

  3. 我知道我可以使用stargazer包将表导出到latex中。但是,我现在需要在表格中插入一个表格。

1 个答案:

答案 0 :(得分:1)

a)因为您可以单独访问输出的每个元素(例如cor.test( )["statistic"]以从相关性中获取t值。),您只需编写一个函数来放置您感兴趣的每个元素。到表中,然后用Markdown输出到Word。

例如,在您的Markdown文档中,假设您拥有以下数据:

title: "stackexchange"
author: "you"
date: "September 13, 2015"
output: word_document
---

```{r setup, include=FALSE, message = FALSE, echo = FALSE}
library(knitr)
set.seed(1)
source( "~/Dropbox/R_Default/MyRTools/DinkyTools/EmptyFrame.R")
df <- data.frame( a = runif( n = 100, min = -1000, max = 1000 ),
              b = rnorm( n = 100, mean = 0, sd = 500 ),
              c = rlogis( n = 100, location = 50, scale = 100 ) )

table_cols <- c( correlation = "character", 
             estimate = "numeric",
             t_value = "numeric", 
             p_value = "numeric", 
             CI_l_95 = "numeric", 
             CI_u_95 = "numeric" )

nvar <- c(1:3)
cmb <- combn( nvar , m = 2)

然后使用您喜欢的任何方法为您的表创建一个空数据框。我有自己的脚本来创建空数据框,但无论如何你都可以这样做:

tbl <- EmptyFrame( rows = ncol( cmb ), varnames_and_types = table_cols ,     print.opt = TRUE) 

>correlation estimate t_value p_value CI_l_95 CI_u_95
1        <NA>       NA      NA      NA      NA      NA
2        <NA>       NA      NA      NA      NA      NA
3        <NA>       NA      NA      NA      NA      NA
[1] "correlation  :  character"
[1] "estimate  :  numeric"
[1] "t_value  :  numeric"
[1] "p_value  :  numeric"
[1] "CI_l_95  :  numeric"
[1] "CI_u_95  :  numeric"

然后努力为您的输出编写一些通用函数。例如,如果我喜欢所有成对相关,我会说:

for( ii in 1 : nrow( tbl ) ){
  tbl$correlation[ ii ] <- paste("cor.test of", names( df )[cmb[ 1, ii] ], "     with ", names( df )[cmb[ 2, ii] ] )
  tbl$estimate[ ii ] <- round( as.numeric( cor.test ( df[ , cmb[ 1, ii ] ], df[ , cmb[2 , ii] ] )[[ "estimate" ]] ), 2)
  tbl$t_value[ ii ] <- round( as.numeric( cor.test ( df[ , cmb[ 1, ii ] ], df[ , cmb[2 , ii] ] )[[ "statistic" ]] )  , 2)
  tbl$p_value[ ii ] <- round( as.numeric( cor.test ( df[ , cmb[ 1, ii ] ], df[ , cmb[2 , ii] ] )[[ "p.value" ]] ), 2)  
  tbl$CI_l_95[ ii ] <- round( cor.test ( df[ , cmb[ 1, ii ] ], df[ , cmb[2 , ii] ] )[[ "conf.int" ]][1] , 2)
  tbl$CI_u_95[ ii ] <- round( cor.test ( df[ , cmb[ 1, ii ] ], df[ , cmb[2 , ii] ] )[[ "conf.int" ]][2], 2)
}
```  

这为您提供了一个表,它将在Markdown中编译成单词。

```{r}
kable( tbl )  
|correlation            | estimate| t_value| p_value| CI_l_95| CI_u_95|
|:----------------------|--------:|-------:|-------:|-------:|-------:|
|cor.test of a  with  b |     0.09|    0.88|    0.38|   -0.11|    0.28|
|cor.test of a  with  c |    -0.17|   -1.74|    0.08|   -0.36|    0.02|
|cor.test of b  with  c |    -0.02|   -0.23|    0.82|   -0.22|    0.17|

```

b)是的。通常,您使用apply(X, MARGIN, FUN, ...)在数据框的所有列上运行函数。

c)在我看来,你会非常喜欢使用Markdown

相关问题