如何将不同的聚合函数应用于R中的不同列?

编程入门 行业动态 更新时间:2024-10-27 14:25:10
本文介绍了如何将不同的聚合函数应用于R中的不同列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何将不同的聚合函数应用于R中的不同列? aggregate()函数仅提供一个要传递的函数参数:

How can I apply different aggregate functions to different columns in R? The aggregate() function only offers one function argument to be passed:

V1 V2 V3 1 18.45022 62.24411694 2 90.34637 20.86505214 1 50.77358 27.30074987 2 52.95872 30.26189013 1 61.36935 26.90993530 2 49.31730 70.60387016 1 43.64142 87.64433517 2 36.19730 83.47232907 1 91.51753 0.03056485 ... ... ... > aggregate(sample,by=sample["V1"],FUN=sum) V1 V1 V2 V3 1 1 10 578.5299 489.5307 2 2 20 575.2294 527.2222

如何将 different 函数应用于每列,即,将V2与mean()函数聚合,将V2与sum()函数聚合,而无需多次调用aggregate() ?

How can I apply a different function to each column, i.e. aggregate V2 with the mean() function and V2 with the sum() function, without calling aggregate() multiple times?

推荐答案

对于该任务,我将在plyr

> library(plyr) > ddply(sample, .(V1), summarize, V2 = sum(V2), V3 = mean(V3)) V1 V2 V3 1 1 578.5299 48.95307 2 2 575.2294 52.72222

更多推荐

如何将不同的聚合函数应用于R中的不同列?

本文发布于:2023-07-10 08:31:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1089850.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用于   如何将   函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!