0
Functions$diversity <- function(db,entity,trait,v){
  db %>%
summarize(crossed = list(crossing(trait, trait)),
          .by = entity) %>%
  unnest(crossed) %>%
  rename(
    i = trait...1,
    j = trait...2
  ) %>%
  left_join(db %>% transmute(
      entity,
      i = trait,
      p_i = v
    )
  ) %>%
    left_join(db %>% transmute(
      entity,
      j = trait,
      p_j = v
    )
  )
}

Here I cannot manage how make the function understand that

  • the argument db is indeed a tibble, but the arguments entity, trait and v are columns of entity.
  • trait...1 is the transitory name of a variable of the unnest

I know that I should use paste, glue or targets but everything that I try does not seem to work.

UPDATE:

f <- function(db, entity, trait) {
  
  db %>%
    summarise(i = crossing({{ trait }},{{ trait }}),
              .by = {{ entity }}) 
}

db <- data.frame(
  entity = c("A", "A", "B", "B"),
  beta = c("X", "Y", "X", "Z")
)

f(db,entity,beta)

I think that crossing does not work even with curly-curly? Can you confirm this to me?

GiulioGCantone
  • 195
  • 1
  • 10
  • You are right. According to [this issue](https://github.com/tidyverse/tidyr/issues/1397), it does not support this well for now. – Liang Zhang Jul 19 '23 at 01:38

0 Answers0