计算“切割”数据的均值和四分位数范围以作图

时间:2019-06-24 12:32:08

标签: r

抱歉,我是R新手,例如,我有一个具有树的高度和树冠密度的数据集:

allprojects {
  // ...
  configurations.all {
    resolutionStrategy {
      force 'com.google.firebase:firebase-common:17.0.0'
      force 'com.google.android.gms:play-services-basement:16.2.0'
      force 'com.google.firebase:firebase-iid:16.0.0'
      force 'com.google.firebase:firebase-auth:17.0.0'
    }
  }
}

我想将“ h_100”重新分组为2m个间隔(从2m最小到最大30m),然后我想计算每个间隔的平均i_cd值和四分位数范围,以便我可以用最小二乘回归绘制它们。我用来获取均值的代码有问题。这是我到目前为止的内容:

i_h100   i_cd
2.89     0.0198
2.88     0.0198
17.53    0.658
27.23    0.347

预先感谢您的任何建议。

2 个答案:

答案 0 :(得分:2)

使用aggregate()计算分组均值。

# Some example data
set.seed(1)

i_h100 <- round(runif(100, 2, 30), 2)
i_cd <- rexp(100, 1/i_h100)
mydata <- data.frame(i_cd, i_h100)

# Grouping i_h100
mydata$i_h100_2m <- cut(mydata$i_h100, seq(2, 30, by=2))
head(mydata)
#        i_cd i_h100 i_h100_2m
# 1  2.918093   9.43    (8,10]
# 2 13.735728  12.42   (12,14]
# 3 13.966347  18.04   (18,20]
# 4  2.459760  27.43   (26,28]
# 5  8.477551   7.65     (6,8]
# 6  6.713224  27.15   (26,28]

# Calculate groupwise means of i_cd
i_cd_2m_mean <- aggregate(i_cd ~ i_h100_2m, mydata, mean)

# And IQR
i_cd_2m_iqr <- aggregate(i_cd ~ i_h100_2m, mydata, IQR)

upper <- i_cd_2m_mean[,2]+(i_cd_2m_iqr[,2]/2)
lower <- i_cd_2m_mean[,2]-(i_cd_2m_iqr[,2]/2)

# Plotting the result
plot.default(i_cd_2m_mean, xaxt="n", ylim=range(c(upper, lower)),
  main="Groupwise means \U00B1 0.5 IQR", type="n")
points(upper, pch=2, col="lightblue", lwd=1.5)
points(lower, pch=6, col="pink", lwd=1.5)
points(i_cd_2m_mean, pch=16)

axis(1, i_cd_2m[,1], as.character(i_cd_2m[,1]), cex.axis=0.6, las=2)

enter image description here

答案 1 :(得分:1)

这是一个解决方案,

library(reshape2)
library(dplyr)
mydata <- data_frame(i_h100=c(2.89,2.88,17.53,27.23),i_cd=c(0.0198,0.0198,0.658,0.347))
     height <- mydata$i_h100
breaks  <- seq(2,30,by=2)  #2m intervals
height.cut <- cut(height, breaks, right=TRUE)

mydata$height.cut <- height.cut

mean_i_h100 <- mydata %>% group_by(height.cut) %>% summarise(mean_i_h100 = mean(i_h100))

一些评论:

  • 最好避免使用函数名来命名变量,因此我将mean变量更改为mean_i_h100
  • 我正在使用管道符号,这使代码更具可读性,它避免了重复每个函数的第一个参数,您可以找到a more detailed explanation here。 没有管道符号,最后一行代码将是: mean_i_h100 <- summarise(group_by(mydata,height.cut),mean_i_h100 = mean(i_h100))
  • 您必须加载用library安装的两个软件包