首页 > 解决方案 > R中的动态函数名称

问题描述

我需要做的是在循环中同时使用 R 中的which.minwhich.max函数。

我可以只使用一个if声明(即if find_max_value == TRUE then which.max(…) else which.min(…)

但我想知道是否有一种方法可以真正使函数名称动态化。例如:

min_or_max =  'max'
special_text = paste('which.',min_or_max,sep='')
special_text(df_results$point)

有什么办法可以使上面的文字起作用吗?

标签: r

解决方案


我们可以invoke使用purrr

library(purrr)
invoke(special_text, list(mtcars$cyl))
#[1] 5

推荐阅读