Question: Due to the structure of my project I have to define my modal's markup in js.
To do so I write:
`var modal_message = '<ul><li id="address"></li><li id="average"></li></ul>';`
However, I need to pass inside this html markup some js variables from my code.

It's kind of `$('#address').innerHTML = var1;
$('#average_sum').innerHTML = var2;`
But this doesnot work as the modal is defined only after clicking a button. So I wanna pre-pass all the variables in the way `<li>{{ var1 }}</li>`
What should I do to achieve it?

Answer: 
Here is a possible solution. I may be over-complicating it, but it should give you the desired result. The first thing that I did was make it so that the strings were not factors in the dataframe. 

```
A1 <- data.frame(Animal1= c("A", "A","B","B","D"), Animal2=c("B","D","D","A","B"), 
                 Frequency=c(2,3,1,4,5), stringsAsFactors = FALSE) 

A1 %>% 
  mutate(combined = map2_chr(Animal1, Animal2, ~paste0(sort(c(.x, .y)), collapse = ""))) %>%  
  group_by(combined) %>% 
  summarise(total = sum(Frequency))

```

output

```
# A tibble: 3 x 2
  combined total
  <chr>    <dbl>
1 AB           6
2 AD           3
3 BD           6

```
Thanks, guys. In addition to @StephenK's answer, I added another step to split the new "combined" column.

```
A1 <- data.frame(Animal1= c("A", "A","B","B","D"), Animal2=c("B","D","D","A","B"), 
             Frequency=c(2,3,1,4,5), stringsAsFactors = FALSE) 

A2<- as.data.frame(A1 %>% mutate(combined = map2_chr(Animal1, Animal2, ~paste0(sort(c(.x, .y)), collapse = ""))) %>%  
group_by(combined) %>% summarise(total = sum(Frequency)))

#create new columns for each letter
A2$Animal1 <- substr(A2$combined, start = 1, stop = 1)
A2$Animal2 <- substr(A2$combined, start = 2, stop = 2)
A2
  combined total Animal1 Animal2
1       AB     6       A       B
2       AD     3       A       D
3       BD     6       B       D

##Select only columns needed and reorder
A3 <- A2[,c("Animal1","Animal2", "total")]
A3
  Animal1 Animal2 total
1       A       B     6
2       A       D     3
3       B       D     6

```