乐趣==';x';不起作用,如何在R中绕过它
我正在尝试编写一个使用其他函数乐趣==';x';不起作用,如何在R中绕过它,r,function,evaluation,R,Function,Evaluation,我正在尝试编写一个使用其他函数FUN作为参数的函数-在这种情况下,我希望(除其他外)更改设置FUN=match0时的操作 library(dplyr) library(purrr) f <- function(df, pair, FUN, ...){ df1 <- df %>% group_split() w <- df1 %>% map(~ .x %>% nrow() %>%
FUN
作为参数的函数-在这种情况下,我希望(除其他外)更改设置FUN=match0时的操作
library(dplyr)
library(purrr)
f <- function(df, pair, FUN, ...){
df1 <- df %>%
group_split()
w <- df1 %>%
map(~ .x %>%
nrow() %>%
seq())
x <- map2(w, df1, ~map(.x, mean, df = .y))
y <- map(x, unlist)
l <- map2(y, df1, ~map(.x, function(x, df = .y){
if(deparse(substitute(FUN)) == 'match0'){
out <- x
} else{
out <- df[x, pair]}
return(out)
}
)) %>% unlist()
df <- bind_rows(df1) %>% bind_cols(index = l)
return(df)
}
库(dplyr)
图书馆(purrr)
f%
nrow()%>%
seq())
函数的第二个参数是df
,在函数中用作data.frame(或矩阵)。字符串'pairs0'
不是对data.frame求值的符号。无论如何,我无法再现错误,我从您发出的呼叫中得到的输出是[1]1 2 3 4 5 6 7 8 9 10
。@RuiBarradas,我将尝试在map2
外部发布一个更现实的示例,函数f
参数FUN
的名称仍然是'match0'
,但在内部它的名称只是'FUN'
。在map2
之前包含cat(“FUN1:”、deparse(substitute(FUN))、“\n”)
,在deparse(substitute()
之前包含相同的内容(更改为FUN2
)
。那么Rui B的建议解决了命名问题了吗?我可能建议您只需使用isItMatch0从代码顶部开始
a <- data.frame(n = c(15,20,15,20,15,20)) %>% group_by(n)
x <- f(a, pair = 'pairs0', FUN = match0)