gpt4 book ai didi

r - 为什么这个简单的 ave 函数不起作用

转载 作者:行者123 更新时间:2023-12-04 09:10:58 24 4
gpt4 key购买 nike

我正在使用以下代码和数据:

> d <- data.frame(year = rep(2000:2002, each = 3), count = round(runif(9, 0, 20)))
> d
year count
1 2000 1
2 2000 4
3 2000 4
4 2001 14
5 2001 8
6 2001 15
7 2002 10
8 2002 14
9 2002 20
>
> with(d, ave(count, year, sum))
Error in unique.default(x) : unique() applies only to vectors

我试过:
> with(d, ave(count, list(year), sum))
Error in unique.default(x) : unique() applies only to vectors
> with(d, ave(count, list('year'), sum))
Error in unique.default(x) : unique() applies only to vectors
>
> with(d, ave(count, 'year', sum))
Error in unique.default(x) : unique() applies only to vectors

> with(d, ave('count', 'year', sum))
Error in unique.default(x) : unique() applies only to vectors
> ave(d$count, d$year, sum)
Error in unique.default(x) : unique() applies only to vectors

> ave(d$count, factor(d$year), sum)
Error in unique.default(x) : unique() applies only to vectors

> ave(d$count, unique(d$year), sum)
Error in unique.default(x) : unique() applies only to vectors

> ave(d$count, factor(unique(d$year)), sum)
Error in unique.default(x) : unique() applies only to vectors
> ave(d$count, as.factor(unique(d$year)), sum)
Error in unique.default(x) : unique() applies only to vectors

以下作品:
> unique(d$count)
[1] 1 4 14 8 15 10 20
> unique(d$year)
[1] 2000 2001 2002

点击、聚合和按工作:
> with(d, tapply(count, year, mean))
2000 2001 2002
3.00000 12.33333 14.66667

> with(d, aggregate(count, list(year), mean))
Group.1 x
1 2000 3.00000
2 2001 12.33333
3 2002 14.66667

> with(d, by(count, year, mean))
year: 2000
[1] 3
-------------------------------------------------------------------------------------------------
year: 2001
[1] 12.33333
-------------------------------------------------------------------------------------------------
year: 2002
[1] 14.66667

为什么会出现错误“unique() 仅适用于向量”以及如何在此处使用 ave 函数?

最佳答案

这有点微妙,我认为最好的文档实际上是在 argument matching 上的 R 语言定义中。 :

The first thing that occurs in a function evaluation is the matchingof formal to the actual or supplied arguments. This is done by athree-pass process:

  1. Exact matching on tags. For each named supplied argument the list offormal arguments is searched for an item whose name matches exactly.It is an error to have the same formal argument match several actualsor vice versa.

  2. Partial matching on tags. Each remaining named suppliedargument is compared to the remaining formal arguments using partialmatching. If the name of the supplied argument matches exactly withthe first part of a formal argument then the two arguments areconsidered to be matched. It is an error to have multiple partialmatches. Notice that if f <- function(fumble, fooey) fbody, then f(f =1, fo = 2) is illegal, even though the 2nd actual argument onlymatches fooey. f(f = 1, fooey = 2) is legal though since the secondargument matches exactly and is removed from consideration for partialmatching. If the formal arguments contain ‘...’ then partial matchingis only applied to arguments that precede it.

  3. Positional matching. Anyunmatched formal arguments are bound to unnamed supplied arguments, inorder. If there is a ‘...’ argument, it will take up the remainingarguments, tagged or not.


所以这是 的特殊性造成的结果。争论。从某种意义上说,它是“贪婪的”,除非您更明确并使用命名参数。这不会在其他地方弹出的原因是因为 通常是最后一个(或接近最后一个)参数,因此在使用位置匹配时通常不会出现这种令人困惑的行为。

关于r - 为什么这个简单的 ave 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26198908/

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