Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Producing the same number of digits in a chunk as inline code

How can I get R chunk code to produce the same number of digits as r inline code in RStudio RMarkdown file?

For example:

```{r}
x=1:30
sigma.sq=sum((x-mean(x))^2)/30
sigma.sq    
```

Thus, $\sigma^2=`r sigma.sq`$.

The chunk produces the number 74.92, but the inline code produces the number 74.9167. I'd like both to be the same.

Suggestions?

D.

P.S. Here's a screen shot of what the above code produces.

enter image description here

like image 544
David Avatar asked Oct 19 '25 04:10

David


1 Answers

round() function gives you an answer. For example, the below inline code in R Markdown would display the second decimal place of the number of the object.

`r round(sigma.sq, 2)`
like image 184
maech Avatar answered Oct 21 '25 18:10

maech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!