gpt4 book ai didi

r - 将 data.frame 折叠为 data.frame——by() 和aggregate() 的问题

转载 作者:行者123 更新时间:2023-12-04 22:34:39 25 4
gpt4 key购买 nike

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

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

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

    关于r - 将 data.frame 折叠为 data.frame——by() 和aggregate() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10013682/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com