blazej - 1 year ago 59

R Question

I posted this question a couple of minutes ago and it got instant minuses so I figured that it was stupid and deleted it.

After reconsideration I still can think of a solution.

This might be due I'm new to R, coding in general, and all those things that are not point and click analysis in SPSS followed by MS Word description of results.

So please forgive me if the answer is basic - I clearly lack intelligence or can't find proper wording for a successful search.

I'm looking for a way to automatically (in order to reduce chance of mistyping errors) pass test results into text (within rmarkdown written in rstudio).

I'm wondering if it's possible to include results from R functions within plain text? If yes, is it matter of markdown formatting or do I need some additional R packages?

For example if I want to describe results of a simple anova

`set.seed(111)`

y = rnorm(18, 0, 1)

x = rnorm(18, 1, 1)

a = c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)

df<- data.frame(a, x, y)

anova<- aov(x~a)

summary(anova)

Df Sum Sq Mean Sq F value Pr(>F)

a 1 0.39 0.3882 0.178 0.678

Residuals 16 34.84 2.1775

Instead of writing by hand:

"We conducted a one way anova that showed no effect of a over x (F(1;16)=0.93; p=0.347 (ns)

I'd like to go with (or anything similar):

"We conducted a one way anova that showed no effect of a over x

`code pasting results in proper format`

I know I can use `r followed by a function to inline simple results, but it's still not clear if this would work for formatted tests and if so, how.

The more general the solution the better - as I'm describing mostly linear models, mixed linear models and lot's of descriptive statistics.

Once again, sorry if it's too basic and not worth answering - I can delete it again if anyone comments so.

Regards

Answer Source

You have to extract the different statistics yourself and combine them. For the anova example you show above, something like this should get you started:

F(`r summary(anova)[[1]][1, "Df"]`; `r summary(anova)[[1]][2, "Df"]`)=`r format(summary(anova)[[1]][1, "F value"], digits = 2, nsmall = 2)`;

In order to extract the statistics, you should have a look at the help files. Often they contain detailed descriptions of the return value. Alternatively you can just have a look at the names of your results. In your example that is `names((anova))`

and `names(summary(anova)[[1]])`

.