R-具有四分位数的Aggregate.Data.Frame

时间:2018-10-16 10:11:36

标签: r

我是R编码的新手(新手),似乎无法弄清楚如何使四分位数输出正确。可能比我发现的要简单得多!

请帮助!

这是我目前拥有的代码,它对于中位数和均值非常有效。

MedianCol = aggregate.data.frame(
  x = list(COLVALUE=COLVALUE),
  by = list(YEAR=YEAR),
  FUN = median,
  na.rm=TRUE
)

MeanCol = aggregate.data.frame(
  x = list(COLVALUE=COLVALUE),
  by = list(YEAR=YEAR),
  FUN = mean,
  na.rm=TRUE
)

#This is where I have an issue, I don't know how to even code it
Q3Col = aggregate.data.frame(
  x = list(COLVALUE=COLVALUE),
  by = list(YEAR=YEAR),
  FUN = quantile(0.25),
  na.rm=TRUE
)

#I will have to add the quartiles in the merge below as well
MovingAverage <- merge(x = MedianCol, y = MeanCol, by = "YEAR", all = TRUE)

Q3Col是我需要返回第三四分位数值的地方。我还需要第一个四分位数,最佳分位数和最差分位数。

我的数据如下所示: Data

提前谢谢!

2 个答案:

答案 0 :(得分:0)

太棒了!做到了。非常感谢!!! 是的,Q3为0.75。我在命名上的错误!

我将meanCol,MedianCol,Q3Col用作“变量”,因为我想将它们输入到单个“表”中。

如何在下面添加Q3Col?我尝试过,但出现错误。 我似乎不能添加超过2。

这有效:

MovingAverage <-merge(x = MedianCol,y = MeanCol,by =“ YEAR”,all = TRUE)

这不是:

MovingAverage <-merge(x = MedianCol,y = MeanCol,z = Q3Col,by =“ YEAR”,全部= TRUE)

答案 1 :(得分:0)

这是我创建的工作脚本。这对我来说可以。在Tibco Spotfire中使用。

MedianCol = aggregate.data.frame(
  x = list(Median=Median),
  by = list(YEAR=YEAR),
  FUN = median,
  na.rm=TRUE
)

Q1Col = aggregate.data.frame(
  x = list(Q1=Q1), 
  by = list(YEAR=YEAR), 
  FUN = quantile, probs = 0.25, 
  na.rm=TRUE 
)

Q3Col = aggregate.data.frame(
  x = list(Q3=Q3), 
  by = list(YEAR=YEAR), 
  FUN = quantile, probs = 0.75, 
  na.rm=TRUE 
)

P10Col = aggregate.data.frame(
  x = list(P10=P10), 
  by = list(YEAR=YEAR), 
  FUN = quantile, probs = 0.10, 
  na.rm=TRUE 
)

P90Col = aggregate.data.frame(
  x = list(P90=P90), 
  by = list(YEAR=YEAR), 
  FUN = quantile, probs = 0.90, 
  na.rm=TRUE 
)

Merge1 <- merge(x = Q1Col, y = Q3Col, by = "YEAR", all = TRUE)
Merge2 <- merge(x = P10Col, y = P90Col, by = "YEAR", all = TRUE)
MergeFinal <- merge(x = Merge1, y = Merge2, by = "YEAR", all = TRUE)
MovingAverage <- merge(x = MergeFinal, y = MedianCol, by = "YEAR", all = TRUE)

这是输出的内容: OUTPUT