对一个向量应用多个函数 [英] Apply many functions to one vector

查看:16
本文介绍了对一个向量应用多个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将多个函数应用于一个向量的选项.我认为这是一种逆应用函数,其中一个函数应用于许多向量(或列).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆