2

Let's take the following dataframe as an example:

df <- data.frame(state = c('up', 'up', 'up', 'down', 'down', 'up', 'down', 'down'), type = c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'))

To count how many times each of the possible combinations of A/B and up/down occur I would do as follows:

a1 <- nrow(subset(subset(df, state == 'up'), type == 'A'))
a2 <- nrow(subset(subset(df, state == 'down'), type == 'A'))
a3 <- nrow(subset(subset(df, state == 'up'), type == 'B'))
a4 <- nrow(subset(subset(df, state == 'down'), type == 'B'))

And to show that in a Rmarkdown table I would do as follows:

| State/type     |   A    |    B   | 
|:--------------:|:------:|:------:|
| Up             | `r a1` | `r a3` |
| Down           | `r a2` | `r a4` |

My question is: is there a better way to do this? This approach becomes very tedious when you have many factor combinations and scales poorly.

Marc P
  • 353
  • 1
  • 17
  • To do what? Count occurrences or show them in table? Those are two very different questions. – pogibas May 18 '18 at 08:07
  • To count occurrences, the `table` solution posted below seems to be the correct approach. Sorry for the ambiguity. – Marc P May 18 '18 at 08:15

1 Answers1

1

We can just use table

tbl <- table(df)

and convert it to a data.frame

as.data.frame.matrix(tbl)

Or if we need the 'state' column as well (instead of row names in the above)

library(tidyverse)
count(df, state, type) %>% 
              spread(type, n)

In the rmarkdown, the above can be used as

```{r echo = FALSE, include = FALSE, cache=FALSE}
df <- data.frame(state = c('up', 'up', 'up', 'down', 'down', 'up', 
   'down', 'down'), type = c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'))
library(dplyr)
library(tidyr)
library(kableExtra)
out <- count(df, state, type) %>% 
              spread(type, n)

```



```{r code1, results = 'asis', echo = FALSE}
kable(out, "html") %>%
  kable_styling()
```
akrun
  • 874,273
  • 37
  • 540
  • 662