Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
乐趣==';x';不起作用,如何在R中绕过它_R_Function_Evaluation - Fatal编程技术网

乐趣==';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)