3

複数の関数を複数の列に順次適用し、複数の列で集計する必要があり、結果をデータ フレームにバインドする必要がある場合は、通常aggregate()、次の方法で使用します。

# bogus functions
foo1 <- function(x){mean(x)*var(x)}
foo2 <- function(x){mean(x)/var(x)}

# for illustration purposes only
npk$block <- as.numeric(npk$block) 

subdf <- aggregate(npk[,c("yield", "block")],
                   by = list(N = npk$N, P = npk$P),
                   FUN = function(x){c(col1 = foo1(x), col2 = foo2(x))})

きれいに並べられたデータ フレームで結果を取得するには、次を使用します。

df <- do.call(data.frame, subdf)

このシナリオで smarterdo.call()を使用して呼び出しを回避したり、最初から別の基本ソリューションを使用してプロセス全体を短縮したりできますか?aggregate()R

4

2 に答える 2