汇总基数R中的数据

时间:2019-03-05 13:19:50

标签: r dplyr nse

我尝试编写一个简单的函数来以聚合级别获取数据框中各列之间的比率。我想获得与通过以下方式获得的输出相同的输出:

library(dplyr)
set.seed(1)
dat <- data.frame(x = rep(1:3, each = 5), a = runif(15, 0, 1), b = runif(15, 0, 2))

oper_fn <- function(df, oper){
  oper <- enquo(oper)
  df %>%
     group_by(x) %>%
     summarize(output = !! oper) %>%
     ungroup()
}

oper_fn(dat, sum(a) / sum(b))

以下内容也应该起作用:

oper_fn(dat, sum(a))

在基数R中执行此操作的方式是什么?

2 个答案:

答案 0 :(得分:2)

您可以只拆分 char c, *head, **res = NULL, *tail; // pointers to parts int nres = 0; // number of results head = chunk.memory; // it begins here const char results[] = "\"results\":["; char *cp = strstr(head, results); // find the results if (!cp) printf("%s not found\n", results), exit(1); cp += strlen(results); // skip to the \n *cp++ = '\0'; // delimit the head do { res = realloc(res, sizeof *res * ++nres); if (!res) puts("out of memory"), exit(1); res[nres-1] = cp; // store the result cp = strstr(cp, "\n}"); // find end of result if (!cp) puts("} not found"), exit(1); cp += 2; // skip over the } c = *cp; // character after } is , or ] *cp++ = '\0'; // delimit the string } while (c == ','); if (c != ']') puts("] not found"), exit(1); tail = cp; // rest follows here // output the parts with results rearranged, e. g. backwards printf("%s\n", head); while (nres--) printf("%s%c", res[nres], nres ? ',' : ']'); free(res); printf("%s", tail); ,然后使用x遍历各组并应用您的功能,即

sapply

答案 1 :(得分:1)

另一个使用aggregate

的选项
tmp <- aggregate(.~x, dat, sum)
cbind(tmp[1], tmp['a']/tmp['b'])

#  x         a
#1 1 0.3448112
#2 2 0.7289661
#3 3 0.5581262

或者将transformaggregate一起使用的班轮

transform(aggregate(.~x, dat, sum), output = a/b)

#  x        a        b    output
#1 1 2.320376 6.729408 0.3448112
#2 2 3.194763 4.382595 0.7289661
#3 3 2.223499 3.983864 0.5581262