- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用以下代码和数据:
> 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
最佳答案
这有点微妙,我认为最好的文档实际上是在 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:
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.
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.
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/
考虑以下 - set.seed(1) x mean 使用 ave、dplyr 和 data.table 方法的组列)。 set.seed(24) df1 % group_b
下面的代码根据group分别产生x和y的组均值。假设我有许多变量要重复相同的操作。 您建议如何继续以通过单个命令获得相同的结果? (我认为有必要采用 tapply,但我不太确定..)。 x=seq(1
我想知道将分位数列添加到数据集的最佳方法是什么。我想为此使用 ave() 函数,例如 ave(iris$Sepal.Length, iris$Species, FUN=quantile) - 但在这种
我正在使用以下代码和数据: > d d year count 1 2000 1 2 2000 4 3 2000 4 4 2001 14 5 2001 8 6
抱歉,如果这看起来微不足道,但在互联网上搜索了一段时间后我无法找到解决方案。 我有一个矩阵和一个与列相关联的因子向量。目标是分别得到所有因子的rowMeans,并保持原来的矩阵结构。所以它可能类似于
我写了一段代码。如果你们能建议更好的方法来做我想做的事情,那就太好了。 dt给出如下: SIC FYEAR AU AT 1 1 2003 6 212.748 2
这篇文章是基于这个问题:Running ifelse statement by a variable 给定一些数据 col1 col2 1 B 10 2 B 7 3 C
R 的 ave() 函数比它的名字所暗示的更有用 - 它基本上是 tapply() 的一个版本,它可以让你返回一个与向量长度相同的向量输入,并将这些值重新设置为与您输入相同的顺序。 > x ave(
我是新来的 R我希望任何人都可以帮助我解决以下问题: 关于以下命令: x$rank <- ave(x$fin, x$unique,FUN=rank) 该命令工作正常,但我想要的是 x$rank 的顺序
我的 MySQL 知识 = 初学者 Columns: MonthlyIncome and MonthlyDebt Table: customer 期望输出:显示 MonthlyIncome - Mon
我正在尝试计算 R 中按多个维度分组的值的滚动平均值。我会按以下方式在 SQL 中执行以下操作: AVG(value) OVER (PARTITION BY dim1, dim2 ORDER B
我在 R 中有一个数据框。示例: d1<-structure(list(A = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L), B = 1:9), .Names
我一直在尝试使用 tapply、ave、ddply 按变量组(年龄、性别)创建统计信息。我无法成功使用上述 R 命令。 library("ff") df <- as.ffdf(data.frame(a
我正在使用此处包含的代码来确定给定值是否为有效日期。在一种特定情况下,它正在评估以下街道地址: 100 112TH AVE NE 显然不是日期,但 Java 将其解释为: Sun Jan 12 00:
我正在 ACE 数据挖掘套件中使用 WARMR 频繁模式算法。这是一个说明我的问题的玩具示例。 假设您有 20 个示例 (example(ex1)),...,example(ex20))),只有一个谓
我是一名优秀的程序员,十分优秀!