当然可以。
library(tidyverse)
df = tribble(
~v, ~w, ~e,
"d", "f", "--",
"d", "a", "->",
"f", "a", "->",
"q", "a", "->")
convert_dag = function(df,V,W,E) {
df %>% mutate(
txt = ifelse(v==V & w==W, paste(V,E,W), paste(v,e,w))
) %>% pull(txt) %>% paste(.,collapse = " ") %>% paste('dag{',.,'}')
}
convert_dag(df, "d", "f", "->")
#[1] "dag{ d -> f d -> a f -> a q -> a }"
更新 1
n = 40
tibble(
id = 1:n,
V = sample(c("d", "f", "q"), n, replace = TRUE),
W = sample(c("a", "f"), n, replace = TRUE),
e = sample(c("--", "->", "<-", "->>", "<<-"), n, replace = TRUE)
) %>% group_by(id) %>%
nest(data=V:e) %>%
mutate(con_dag = map(data, ~convert_dag(df, .x$V, .x$W, .x$e))) %>%
unnest(data:con_dag)
输出
# A tibble: 40 x 5
# Groups: id [40]
id V W e con_dag
<int> <chr> <chr> <chr> <chr>
1 1 q f ->> dag{ d -- f d -> a f -> a q -> a }
2 2 f a -> dag{ d -- f d -> a f -> a q -> a }
3 3 f f -- dag{ d -- f d -> a f -> a q -> a }
4 4 f a -> dag{ d -- f d -> a f -> a q -> a }
5 5 q a ->> dag{ d -- f d -> a f -> a q ->> a }
6 6 q a <<- dag{ d -- f d -> a f -> a q <<- a }
7 7 q f <<- dag{ d -- f d -> a f -> a q -> a }
8 8 f f <- dag{ d -- f d -> a f -> a q -> a }
9 9 d a -> dag{ d -- f d -> a f -> a q -> a }
10 10 q a -> dag{ d -- f d -> a f -> a q -> a }
# ... with 30 more rows
注意,由于tibble 突变的特定构造,它必须包含一个分组变量。这里是变量id。
更新 2
tibble(
id = 1:2,
V = c('d','d'),
W = c('f','a'),
e = c('->','<-')
) %>% group_by(id) %>%
nest(data=V:e) %>%
mutate(con_dag = map(data, ~convert_dag(df, .x$V, .x$W, .x$e))) %>%
unnest(data:con_dag)
输出
# A tibble: 2 x 5
# Groups: id [2]
id V W e con_dag
<int> <chr> <chr> <chr> <chr>
1 1 d f -> dag{ d -> f d -> a f -> a q -> a }
2 2 d a <- dag{ d -- f d <- a f -> a q -> a }
现在清楚了吗?
更新 3
convert_dag2 = function(df,V,W,E) {
df %>% mutate(
test = v %in% V & w %in% W,
txt = ifelse(test, paste(V,E,W), paste(v,e,w))
) %>% pull(txt) %>% paste(.,collapse = " ") %>% paste('dag{',.,'}')
}
convert_dag2(df, c('d','d'), c('f','a'), c('->','<-'))
# [1] "dag{ d -> f d <- a f -> a q -> a }"