Экспорт результатов из R в Word

По тем или иным причинам нам часто нужно экспортировать результаты нашего анализа, проведённого в R (таблицы, графики и т.п.) в текстовый редактор MS Word. Есть несколько методов, вот один из них. Рассматривается пример экспорта результатов регрессионного анализа в Ворд.

Get the results from Cox Regression Analysis

As an example to illustrate this post, I will compute a survival analysis. Survival analysis is statistical methods for analyzing data where the outcome variable is the time until the occurrence of an event. The event can be a occurrence of a disease or death, etc. In R we compute the survival analysis with the survival package. The function for Cox regression analysis is coxph(). I will use the veteran data which come with survival package.

Now let say that we are interested to know the risk of dying (status) from different cell type (celltype) and treatment (trt) when we adjust for other variables (karno, age prior, diagtime).

This is the model:

And the output:

As we see there are “a lot” of results. In manuscript we often report only the Hazard ratio and 95% Confidence interval and only for the variables of interest. For example in this case I am interested for the cell types and treatment. Note: I will not comment for the regression coefficients since is not the aim of this post.

Prepare the table by creating the columns

Select variables of interest from the table

As I mentioned earlier, I am interested only for 2 variables (cell type and treatment). With the code below I will select those variables.

Format the table

In the manuscript we present the confidence intervals within brackets. Therefore, with the code below I will add the brackets.

Finalize the table and make it ready for Microsoft Word

The table is almost ready, now I will merge in one column by using package tidyr with function unite().

Export Table from R to Microsoft Word

To export table from R to Microsoft Word I will use the function FlexTable() from the package ReporteRs. (Credits to the author in StackOverflow).

This is the table in Microsoft Word:

R to docx

Источник

Data Scientist # 1

Машинное обучение, большие данные, наука о данных, анализ данных, цифровой маркетинг, искусственный интеллект, нейронные сети, глубокое обучение, data science, data scientist, machine learning, artificial intelligence, big data, deep learning

Данные — новый актив!

Эффективно управлять можно только тем, что можно измерить.
Copyright © 2016-2021 Data Scientist. Все права защищены.