改变tapply中的参数?

发布于 2024-12-22 13:35:32 字数 662 浏览 1 评论 0原文

我有几个组,比方说 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木緿 2024-12-29 13:35:32

我将在这里戴上我的读心帽,并尝试一下你想要这样的东西:

dat <- data.frame(x = runif(100),grp = rep(letters[1:3],length.out = 100))

mapply(cut,split(dat$x,dat$grp),list(c(-Inf,0.5,Inf),
                                     c(-Inf,0.1,0.5,0.9,Inf),
                                     c(-Inf,0.25,0.5,0.75,Inf)))

所以这只是将 x 分割为 grp 并应用 剪切到每个部分,对每个部分使用不同的中断。

I'm going to put my mind-reading hat on here and take a stab that you want something like this:

dat <- data.frame(x = runif(100),grp = rep(letters[1:3],length.out = 100))

mapply(cut,split(dat$x,dat$grp),list(c(-Inf,0.5,Inf),
                                     c(-Inf,0.1,0.5,0.9,Inf),
                                     c(-Inf,0.25,0.5,0.75,Inf)))

So this is simply splitting x by grp and applying cut to each piece using different breaks for each piece.

临走之时 2024-12-29 13:35:32

实际上R在这里表现得很聪明。我找到了一个解决方案,它确实按照我最初的想法工作。尽管它没有使用 apply 系列。不知何故,R 在这里创建整数而不是因子 - 这就是为什么在这个解决方案中,像 Joran 提到的因子级别没有问题。

dat <- data.frame(x = rnorm(100),grp = rep(letters[1:3],length.out = 100))
ifelse(dat$grp == "a",cut(dat$x,breaks=c(-Inf,0.1,0.2,Inf)),
       ifelse(dat$grp == "b",cut(dat$x,breaks=c(-Inf,0.1,1,Inf)),
              cut(dat$x,breaks=c(-Inf,0.9,2,Inf))) )

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.

dat <- data.frame(x = rnorm(100),grp = rep(letters[1:3],length.out = 100))
ifelse(dat$grp == "a",cut(dat$x,breaks=c(-Inf,0.1,0.2,Inf)),
       ifelse(dat$grp == "b",cut(dat$x,breaks=c(-Inf,0.1,1,Inf)),
              cut(dat$x,breaks=c(-Inf,0.9,2,Inf))) )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文