【问题标题】:Using R to calculate the mean of the part of a vector [duplicate]使用R计算向量部分的平均值[重复]
【发布时间】:2019-10-01 23:36:05
【问题描述】:

我的R 向量如下所示:

vector <- c(3, 2, 1, 4, 6, 2, 7)

我想使用函数tapply() 从向量中计算first 4 numbermean。我该怎么做?

我做了什么?

tapply(vector(1,4), mean)

但它似乎不这样工作。

我们需要使用tapply()函数。

【问题讨论】:

  • @thelatemail 我的问题需要tapply(),但您标记的duplicate 不使用tapply()
  • 我没有投反对票,但您应该包括预期的输出。我假设2.5 但你对tapply() 的坚持让我质疑这个假设。另外,运行vector(1,4)会得到什么?
  • @Cole 我收到此错误:Error in unique.default(x, nmax = nmax) : unique() applies only to vectors 但我找到了解决方案并找到了answered it below
  • Solution: 先分类吧。 temp_vector = c(1,1,1,1,2,2,2),然后是 tapply(vector, temp_vector, mean),这将为您提供 2.5, 5.0。正是我正在寻找的答案。

标签: r tapply


【解决方案1】:

你可以这样做

mean(vector[1:4])

【讨论】:

    猜你喜欢
    • 2015-11-02
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    相关资源
    最近更新 更多