ref_tables <- tibble(`col 1` = letters[1:5], `col 2` = letters[6:10], `col 3` = letters[11:15])
`col 1` `col 2` `col 3`
<chr> <chr> <chr>
1 a f k
2 b g l
3 c h m
4 d i n
5 e j o
index_selection <- c("col 1", "col 2", "col 3")
我想创建一个定制函数,应用于REF_TABLES的每一行.我想在名为DFS的第四栏中看到结果.
这是一个很普通的函数:
f <- function(x) {
return(paste(x, collapse = " "))
}
如果我使用这样的反号显式地输入列名,我会得到我所期望的结果.
d <- ref_tables %>%
rowwise() %>%
mutate(dfs = list(f(c(`col 1`, `col 2`, `col 3`))))
# d$dfs[1] returns the result i'm expecting "a f k".
但我想使用字符向量index_select.
d <- ref_tables %>%
rowwise() %>%
mutate(dfs = list(f(index_selection)))
# d$dfs[1] returns a result I am not expecting. "col 1 col 2 col 3".
我try 使用!!,!!,sym()的组合,但我想不出如何做到这一点.有谁能帮忙吗?