0
votes

I have a dataframe with the following list-column Fruits:

Fruits

Apple, Pear, Banana

Pear

Banana, Apple

Apple, Pear

Watermelon, Apple, Pear, Banana

Pear, Watermelon


I would like to create a cross matrix in R Markdown - for just this one column so the output would show which fruits are closely listed together. The x and y axis are both based on this one column.

2

2 Answers

0
votes

I think that what you want to get is the covariance matrix i.e:

df %>% 
  mutate(.id=row_number()) %>%
  unnest_longer(c(Fruits)) %>%
  mutate(value = 1) %>%
  pivot_wider(id_cols=.id, names_from=Fruits) %>%
  select(-.id) -> result
# replacing NA s with 0 s
result[is.na(result)] <- 0

result
# A tibble: 6 x 4
  Apple  Pear Banana Watermelon
  <dbl> <dbl>  <dbl>      <dbl>
1     1     1      1          0
2     0     1      0          0
3     1     0      1          0
4     1     1      0          0
5     1     1      1          1
6     0     1      0          1
result %>% cov
                 Apple        Pear Banana  Watermelon
Apple       0.26666667 -0.06666667    0.2 -0.06666667
Pear       -0.06666667  0.16666667   -0.1  0.06666667
Banana      0.20000000 -0.10000000    0.3  0.00000000
Watermelon -0.06666667  0.06666667    0.0  0.26666667

data

df <- tibble(Fruits=list(list("Apple", "Pear", "Banana"),
list("Pear"),
list("Banana", "Apple"),
list("Apple", "Pear"),
list("Watermelon", "Apple", "Pear", "Banana"),
list("Pear", "Watermelon")))
0
votes

Taking what Abdessabour Mtk said and wrap it inside Rmarkdown code with a YAML header.

---
output: 
  html_document
---

```{r}
library(tidyverse)
[R code goes here]
```