简体   繁体   English

从R导出数据到Word格式的表格

[英]Export data to table in Word format from R

I have encountered this problem for multiple times.我多次遇到这个问题。 I want to export a summary data set that I made from R to table in Word.我想将我从R制作的汇总数据集导出到 Word 中的表格中。 The best I can do for now is to first export the data to Excel, and then copy the table in Excel to word.目前我能做的最好的就是先把数据导出到Excel,然后把Excel里面的表格复制到word中。

My sample data:我的示例数据:

> sum_tab
        col1 col2 col3
2    move up   10   10
3  no change    4    9
1  move down   12    7
21   move up   11    5
31 no change    4   16
11 move down   11    5
22   move up    9    6
32 no change   10   14
12 move down    7    6

Export to Excel:出口到Excel:

library(xlsx)
write.xlsx(sum_tab, file = "sum_tab.xlsx")

Is there a neat way to export the sum_tab data to table in Word with 10 rows and 4 columns?有没有一种巧妙的方法可以将sum_tab数据导出到 10 行 4 列的 Word 表格中?

You can use one of these two options, use rmarkdown or the sjPlot package 您可以使用这两个选项之一,使用rmarkdown或sjPlot包

sum_tab = data.frame(col1 = c("move up","no change", "move down", "move up", "no change","move down","move up","no change","move down"), 
                     col2 = c(10,4,12,11,4,11,9,10,7), col3 = c(10,9,7,5,16,5,6,14,6))
row.names(sum_tab) <- c(2,3,1,21,31,11,22,32,12)
sum_tab
library(sjPlot)
tab_df(sum_tab)

In the viewer you can select the table with the cursor and paste it in Word. 在查看器中,您可以使用光标选择表并将其粘贴到Word中。

The sjPlot has updated its functions with the important file parameter with which you can specify a .doc file (note: not .docx ). sjPlot 已使用重要的file参数更新其功能,您可以使用该参数指定.doc文件(注意:不是.docx )。

So to save the sum_tab dataframe, you merely write:所以要保存sum_tab dataframe,你只需写:

sjPlot::tab_df(sum_tab, file = "output.doc")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM