将多个函数应用于一个向量 [英] Apply many functions to one vector
本文介绍了将多个函数应用于一个向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找将多个函数应用于一个向量的选项.我认为这是对逆应用函数的一种,其中一个函数适用于许多向量(或列).
I am looking for an option to apply many functions to one vector. I think it is kind on an inverse apply function where one function is applied to many vectors (or columns).
是否可以指定两个或多个函数(例如均值和最大值)并将其应用于向量?
Is there a way of specifing two or more functions (like mean and max) and apply this on a vector?
推荐答案
与@CathG的注释类似,但没有get
:
Similar to @CathG's comment, but without get
:
v <- rnorm(10)
funs <- list(mean, median, sd)
sapply(funs, function(fun, x) fun(x), x = v)
或使用do.call
:
sapply(funs, do.call, args = list(v))
这篇关于将多个函数应用于一个向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文