从功能中发送列名到ddply [英] Sending in Column Name to ddply from Function

查看:55
本文介绍了从功能中发送列名到ddply的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够将列名发送给我正在向ddply发起的呼叫. ddply呼叫示例:

I'd like to be able to send in a column name to a call that I am making to ddply. An example ddply call:

ddply(myData, .(MyGrouping), summarise, count=sum(myColumnName))

如果我将ddply包装在另一个函数中,是否可以包装它,以便我可以将myColumnName的任意值传递给调用函数?

If I have ddply wrapped within another function is it possible to wrap this so that I can pass in an arbitrary value as myColumnName to the calling function?

推荐答案

必须有更好的方法.而且我不知道如何使它与摘要结合使用.

There has got to be a better way. And I couldn't figure out how to make it work with summarise.

my.fun <- function(df, count.column) { 
  ddply(df, .(x), function(d) sum(d[[count.column]]))
}

dat <- data.frame(x=letters[1:2], y=1:10)

> my.fun(dat, 'y')
  x V1
1 a 25
2 b 30
> 

这篇关于从功能中发送列名到ddply的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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