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 atibble
, but the argumentsentity
,trait
andv
are columns ofentity
. trait...1
is the transitory name of a variable of theunnest
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?