How to generate conditional words in the text? (Inline code)

I want to have some conditional words in my R Markdown document. Depending of the outcome in some of the calculations from the tables different words should show up in the ordinary text. Please, see my example below:

The table (a chunk):

testtabell <- matrix(c(32, 33, 45, 67, 21, 56, 76, 33, 22), ncol=3,byrow = TRUE)
colnames(testtabell) <- c("1990", "1991", "1992")
rownames(testtabell) <- c("Region1", "Region2", "Region3")
testtabell <- as.table(testtabell)
testtabell

This should be in the inline code and generate different word options in the regular text flow in the RMD:

r if testtabell[2,2]-[2,1] < testtabell[3,2]-testtabell[3,1] then type "under" or else "above"

Did you miss "testtabell" before "[2,1]" here?

In that case, use the ifelse function, as follows.

---
title: "try"
output: pdf_document
---

some text

```{r}
testtabell <- matrix(c(32, 33, 45, 67, 21, 56, 76, 33, 22), ncol=3,byrow = TRUE)
colnames(testtabell) <- c("1990", "1991", "1992")
rownames(testtabell) <- c("Region1", "Region2", "Region3")
testtabell <- as.table(testtabell)
testtabell
```

some text

some text `r ifelse(testtabell[2, 2] - testtabell[2, 1] < testtabell[3, 2] - testtabell[3, 1], "under", "above")` some test

some text

This generates try.pdf (96.4 KB), and most probably that's what you are looking for.

1 Like

Thanksgiving you so much!

If your problem is solved, will you please mark the question solved by choosing the solution?

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.