改变tapply中的参数?
我有几个组,比方说 A、B、C,我想根据这些组剪切
另一个变量,即每个组对同一变量都有特定的中断。
如果我必须计算组的平均值,我会像这样使用tapply
:
tapply(mydata$var,mydata$group,mean)
不幸的是,我不知道如何通过更改breaks=c(.. .) 不同群体的论据。
tapply(mydata$var,mydata$group,cut)
有什么建议吗?我想使用 tapply
来完成此操作,但除定制函数之外的任何其他解决方案也都适合。
编辑:一些小例子:
test <- data.frame(var=rnorm(100,0,1),
group=c(rep("A",30),
rep("B",20),
rep("C",50)))
# for group A:
cut(test$var,breaks=c(-4,0,4))
# for group B
cut(test$var,breaks=c(-4,1,4))
等等......
I have a several groups, let's say A,B,C and I want to cut
another variable based on these groups, i.e. each group has specific breaks for the same variable.
If I had to calculate the groups mean, i´d use tapply
like this:
tapply(mydata$var,mydata$group,mean)
Unfortunately I do not know how to fix this for cut
with changing breaks=c(...) arguments for different groups.
tapply(mydata$var,mydata$group,cut)
Any suggestions? I´d like to do it with tapply
but any other solution but a custom made function would be suitable too.
EDIT: some small example:
test <- data.frame(var=rnorm(100,0,1),
group=c(rep("A",30),
rep("B",20),
rep("C",50)))
# for group A:
cut(test$var,breaks=c(-4,0,4))
# for group B
cut(test$var,breaks=c(-4,1,4))
and so on...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将在这里戴上我的读心帽,并尝试一下你想要这样的东西:
所以这只是将
x
分割为grp
并应用剪切
到每个部分,对每个部分使用不同的中断。I'm going to put my mind-reading hat on here and take a stab that you want something like this:
So this is simply splitting
x
bygrp
and applyingcut
to each piece using different breaks for each piece.实际上R在这里表现得很聪明。我找到了一个解决方案,它确实按照我最初的想法工作。尽管它没有使用
apply
系列。不知何故,R 在这里创建整数而不是因子 - 这就是为什么在这个解决方案中,像 Joran 提到的因子级别没有问题。Actually R behaves quite clever here. I found a solution that does work the way I thought initially. Though it's not using the
apply
family. Somehow R creates integers here instead of factors – which is why in this solution, there is no problem with factor levels like Joran mentions.