我有一个包含213行和2列(日期和概率)的数据框。多亏了上一篇文章,我可以按季度对日期进行分组,以减少行数。问题在于,现在,第二列 Probability (概率)由我要相应平均的数字组成。
让我们举个例子。
Date <- c("2000-01-05", "2000-02-03", "2000-03-02", "2000-03-30", "2000-04-13", "2000-05-11", "2000-06-08", "2000-07-06", "2000-09-14", "2000-10-05", "2000-10-19", "2000-11-02", "2000-12-14")
Article <- c(0.5, 1, 0.3, 0.8, 0.7, 2, 3, 1.5, 1, 2, 0.6, 0.5, 0.5)
Date <- data.frame(Date)
Article <- data.frame(Article)
df <- cbind(Date, Article)
#Dataframe
Date Probability
1 2000-01-05 0.5
2 2000-02-03 1
3 2000-03-02 0.3
4 2000-03-30 0.8
5 2000-04-13 0.7
6 2000-05-11 2
7 2000-06-08 3
8 2000-07-06 1.5
9 2000-09-14 1
10 2000-10-05 2
11 2000-10-19 0.6
12 2000-11-02 0.5
13 2000-12-14 0.5
我想要获得的最终输出如下:
Date Probability
1 2000 Q1 0.65
2 2000 Q2 1.9
3 2000 Q3 1.25
4 2000 Q4 0.9
从本质上讲,行已按季度分组在一起,并且已对关联的数字进行平均。
不幸的是,我不知道该怎么做。
有人可以帮我吗?
谢谢!
答案 0 :(得分:1)
Base R解决方案:
for ele in self.stackView.arrangedSubviews {
if let viewWithTag = self.view.viewWithTag(100) {
viewWithTag.removeFromSuperview()
}
}
数据:
# Summarise the dataframe:
summary_df <- aggregate(list(Probability = df$Article),
by = list(Date = paste(gsub("[-].*", "", df$Date), quarters(df$Date), sep = " ")),
FUN = mean)
答案 1 :(得分:1)
将data.table
与lubridate
组合:
代码
require(data.table); require(lubridate)
setDT(df)
df = df[, .(MeanProb = mean(Article)), .(Qtr = as.yearqtr(as.Date(Date)))]
输出
> df
Qtr MeanProb
1: 2000 Q1 0.65
2: 2000 Q2 1.90
3: 2000 Q3 1.25
4: 2000 Q4 0.90