微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

对于每个组,应用一个函数并返回R

如何解决对于每个组,应用一个函数并返回R

我正在使用R。我有一个data.frame,看起来像这样:

a <- c(1,1,2,2)
b <- c(10,20,30,40,50,60)
y <- c(2000,2000,2001,2002,2002)
c <- cbind(b,a,y)
colnames(c) <- c("b","a","y")

要做的就是将sum函数应用于一组ay。与输入相比,输出可能具有不同的dim

我希望得到以下结果:

aa <- c(1,2)
yy <- c(2000,2002)
bb <- c(30,110)
result <- cbind(bb,aa,yy)

我尝试了以下操作,但收效甚微:

as_tibble(c) %>% group_by(a,y) %>% mutate(tapply(,FUN = sum(b)))

dplyr之外也有提示吗?预先非常感谢。

解决方法

我认为您需要以下条件:

(lldb) help continue
Continue execution of all threads in the current process.

Syntax: continue <cmd-options>

Command Options Usage:
  continue [-i <unsigned-integer>]

       -i <unsigned-integer> ( --ignore-count <unsigned-integer> )
            Ignore <N> crossings of the breakpoint (if it exists) for the currently selected thread.

'continue' is an abbreviation for 'process continue'

您可以使用基数R获得相同的结果:

break modify -i 10 <BKPTNO>

,

使用data.table

library(data.table)
as.data.table(c)[,lapply(.SD,sum),.(y,a)]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。