【问题标题】:How can I get the average of values based on the range of values from other variable using R? [duplicate]如何使用 R 根据其他变量的值范围获得值的平均值? [复制]
【发布时间】:2017-12-26 00:32:42
【问题描述】:

例如 我有两个变量 R 和 T

T   R
0   0.626290968
0.1 0.447799532
0.2 0.408269444
0.3 0.383617332
0.4 0.364870538
0.5 0.352411299
0.6 0.332493421
0.7 0.328406709
0.8 0.314931307
0.9 0.326861296
1   0.337731233

我想根据 R 变量中 0.3-0.9 的范围来估计 T 的平均值 提前谢谢

【问题讨论】:

  • 使用cut 创建一个变量并使用它来分组并使用aggregatedplyrdata.table 获取mean

标签: r statistics mean


【解决方案1】:
library(dplyr)

df %>%
  filter(T >= .3, T <= .9) %>%
  summarise(mean = mean(R))

【讨论】:

    【解决方案2】:
    > with(Df, mean(R[T >= 0.3 & T <= 0.9]) )
    

    aggregate(Df$R, list(cut(Df$T, breaks=c(0.3,0.9))), mean)
    

    【讨论】:

      猜你喜欢
      • 2021-06-23
      • 1970-01-01
      • 2020-06-05
      • 2017-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-27
      相关资源
      最近更新 更多