将data.frame折叠为data.frame--by()和aggregate()的问题
假设我有以下返回我喜欢的汇总统计信息的数据和函数将data.frame折叠为data.frame--by()和aggregate()的问题,r,R,假设我有以下返回我喜欢的汇总统计信息的数据和函数 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,
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
- minPct
我该怎么做呢?看看
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
理想情况下,您的函数显式返回一个
数据.frame
,但在这种情况下,它可以轻松正确地强制为一个。aggregate
是为不同的目的而设计的。您需要的是lappy(split())
:
在输出可预测为常规的情况下,最好使用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
>ttbl ttbl ttbl
国家委员会
亚洲0.0020 0.08215838 0.70
巴西0.0020 0.05700877 0.40
美国-0.0224 0.41093795 0.05
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
这些并没有给我实际应用程序中进一步后处理所需的data.frame
。将as.data.frame(t(sappy())
应用得很接近,但当然缺少国家栏。
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