将数据帧折叠为数据帧--by()和aggregate()存在问题



考虑一下我有以下数据和函数返回我喜欢的汇总统计信息

landlines <- data.frame(
                year=rep(c(1990,1995,2000,2005,2010),times=3),
                country=rep(c("US", "Brazil", "Asia"), each=5),
                pct =  c(0.99, 0.99, 0.98, 0.05, 0.9,
                         0.4,  0.5,  0.55, 0.5,  0.45,
                         0.7,  0.85, 0.9,  0.85, 0.75)
                )
someStats <- function(x)
{
  dp <- as.matrix(x$pct)-mean(x$pct)
  indp <- as.matrix(x$year)-mean(x$year)
  f <- lm.fit( indp,dp )$coefficients
  w <- sd(x$pct)
  m <- min(x$pct)
  results <- c(f,w,m)
  names(results) <- c("coef","sdev", "minPct")
  results
}

我可以成功地将该函数应用于数据子集,如下所示:

> someStats(landlines[landlines$country=="US",])
      coef      sdev    minPct 
 -0.022400  0.410938  0.050000 

或者按国家分类如下:

> by(landlines, list(country=landlines$country), someStats)
country: Asia
      coef       sdev     minPct 
0.00200000 0.08215838 0.70000000 
--------------------------------------------------------------------------------------- 
country: Brazil
      coef       sdev     minPct 
0.00200000 0.05700877 0.40000000 
--------------------------------------------------------------------------------------- 
country: US
     coef      sdev    miPct 
-0.022400  0.410938  0.050000 

问题是,这不是我需要进一步处理的data.frame对象,而且它不会这样投射:

> as.data.frame( by(landlines, list(country=landlines$country), someStats) )
Error in as.data.frame.default(by(landlines, list(country = landlines$country),  : 
  cannot coerce class '"by"' into a data.frame

"没问题!"我想,因为类似的aggregate()函数确实返回了data.frame:

> aggregate(landlines$pct, by=list(country=landlines$country), min)
  country    x
1    Asia 0.70
2  Brazil 0.40
3      US 0.05

问题是,它不能与任意功能正常工作:

> aggregate(landlines, by=list(country=landlines$country), someStats)
Error in x$pct : $ operator is invalid for atomic vectors

我真正想要得到的是一个具有以下列的data.frame对象:

  • 国家
  • 系数
  • sdev
  • 最小Pct

我该怎么做?

查看plyr软件包,尤其是ddply

> ddply(landlines, .(country), someStats)
  country    coef       sdev minPct
1    Asia  0.0020 0.08215838   0.70
2  Brazil  0.0020 0.05700877   0.40
3      US -0.0224 0.41093795   0.05

理想情况下,函数显式返回一个data.frame,但在这种情况下,可以轻松正确地将其强制为一个。

by对象实际上是列表,因此可以在do.call:中使用rbind

do.call("rbind",by(landlines, list(country=landlines$country), someStats))
          coef       sdev minPct
Asia    0.0020 0.08215838   0.70
Brazil  0.0020 0.05700877   0.40
US     -0.0224 0.41093795   0.05

aggregate是为不同的目的而设计的。你想要的是lapply(split()):

> lapply( split(landlines, list(country=landlines$country)), FUN=someStats)
$Asia
      coef       sdev     minPct 
0.00200000 0.08215838 0.70000000 
$Brazil
      coef       sdev     minPct 
0.00200000 0.05700877 0.40000000 
$US
     coef      sdev    minPct 
-0.022400  0.410938  0.050000 

在输出可以预测为规则的情况下,最好使用sapply:

> sapply( split(landlines, list(country=landlines$country)), FUN=someStats)
             Asia     Brazil        US
coef   0.00200000 0.00200000 -0.022400
sdev   0.08215838 0.05700877  0.410938
minPct 0.70000000 0.40000000  0.050000

添加了用行名中的值构造第一列的演示:

> ttbl <- as.data.frame(t(tbl))
> ttbl <- cbind(Country=rownames(ttbl), ttbl)
> ttbl
       Country    coef       sdev minPct
Asia      Asia  0.0020 0.08215838   0.70
Brazil  Brazil  0.0020 0.05700877   0.40
US          US -0.0224 0.41093795   0.05

最新更新