R有相当于Python中reduce()的东西吗? [英] Does R have something equivalent to reduce() in Python?

查看:131
本文介绍了R有相当于Python中reduce()的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即:将两个参数的函数累积地应用到序列项目,从左到右,以便将序列简化为单个值。 >解决方案

是的,它被称为 Reduce



p>

 缩小(粘贴,LETTERS [1:5])
[1]ABCDE

减少(总和,1:5)
[1] 15

#List参数使用相同的
Reduce(sum,list(1,2,3,4,5 ))
[1] 15

有关R中函数式编程的更多信息,请参阅帮助文件?funprog 的别名?Reduce


That is : "Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value. "

解决方案

Yes, it's called Reduce.

An example:

Reduce(paste, LETTERS[1:5])
[1] "A B C D E"

Reduce(sum, 1:5)
[1] 15

#List arguments work the same
Reduce(sum, list(1, 2, 3, 4, 5))
[1] 15

For more information about functional programming in R see the help file for ?funprog, an alias for ?Reduce

这篇关于R有相当于Python中reduce()的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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