- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 R 的新手,遇到了我的第一个困难。我有一个 ca.10000 obs 的数据集。我捕捉事件发生的 365 天。仅在每个月的前 14 天标记出这种情况。我想通过对相应月份之前发生的事件(按小时)进行平均来补充额外的 16 天。
结构如下:
day hours occurrence
2000-01-01 1 5
2000-01-01 2 6
2000-01-01 3 7
... ... ...
2000-01-01 23 3
2000-01-01 24 2
... ... ...
2000-01-02 1 4
2000-01-02 2 2
2000-01-02 3 5
... ... ...
2000-01-02 23 2
2000-01-02 24 1
...
...
2000-01-15 1 average of the previous 1 hours((5+4+n)/2*k))
2000-01-15 2 average of the previous 2 hours ((6+2+n)/2*k))
2000-01-15 3 average of the previous 3 hours((7+5+n)/2*k))
... ... ...
2000-01-15 23 average of the previous 23 hours
2000-01-15 24 average of the previous 24 hours
... ... ...
... ... ...
2000-01-30
2000-01-30
2000-01-30
2000-01-30
... ... ...
... ... ...
2000-02-01
2000-02-01
2000-02-01
2000-02-01
... ... ...
...
... ... ...
2000-12-24
我试过了
aggregate( occurences ~ hours, mean)
但结果毫无意义,我试过了
tapply( X = occurences, INDEX = list(hours), FUN = Mean )
不幸的是,两者都没有像我想象的那样工作。我认为有必要将相应的月份包含在函数中。然而我的手段似乎有限。
最佳答案
你可以试试这个。请注意,为了使示例更小,我只选择每月第 1-4 天和第 0-1 小时的数据。每个月的第 1 天和第 2 天都有发生数据,第 2 天和第 3 天缺少发生数据。
library(dplyr)
# create dummy data
set.seed(123) # for reproducibility of sample
d1 <- data.frame(time = seq(from = as.POSIXct("2000-01-01"),
to = as.POSIXct("2000-02-28"),
by = "hour"))
d1 <- d1 %>%
mutate(hour = as.integer(format(time, "%H")),
day = as.integer(format(time, "%d")), # <~~ only needed to generate sample data
month = as.integer(format(time, "%m")),
occurence = sample(1:10, length(time), replace = TRUE),
occurence = ifelse(day %in% 1:2, occurence, NA)) %>% # <~~~ data only for day 1-2
filter(hour %in% 0:1 & day %in% 1:4) %>% # <~~~ smaller example: select hour 0-1, day 1-4
select(-day)
# calculate mean occurrence per month and hour
d2 <- d1 %>%
group_by(month, hour) %>%
summarise(mean_occ = round(mean(occurence, na.rm = TRUE), 1))
d2
# month hour mean_occ
# 1 1 0 5.0
# 2 1 1 8.0
# 3 2 0 5.5
# 4 2 1 6.5
# replace missing occurrence with mean_occ
d3 <- d1 %>%
left_join(d2, by = c("hour", "month")) %>%
mutate(occurence2 = ifelse(is.na(occurence), mean_occ, occurence)) %>%
select(-month, -mean_occ)
d3
# hour time occurence occurence2
# 1 0 2000-01-01 00:00:00 3 3.0
# 2 1 2000-01-01 01:00:00 8 8.0
# 3 0 2000-01-02 00:00:00 7 7.0
# 4 1 2000-01-02 01:00:00 8 8.0
# 5 0 2000-01-03 00:00:00 NA 5.0
# 6 1 2000-01-03 01:00:00 NA 8.0
# 7 0 2000-01-04 00:00:00 NA 5.0
# 8 1 2000-01-04 01:00:00 NA 8.0
# 9 0 2000-02-01 00:00:00 4 4.0
# 10 1 2000-02-01 01:00:00 6 6.0
# 11 0 2000-02-02 00:00:00 7 7.0
# 12 1 2000-02-02 01:00:00 7 7.0
# 13 0 2000-02-03 00:00:00 NA 5.5
# 14 1 2000-02-03 01:00:00 NA 6.5
# 15 0 2000-02-04 00:00:00 NA 5.5
# 16 1 2000-02-04 01:00:00 NA 6.5
关于r - 如何按小时计算一段时间内的平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27404700/
我有一个大小为 320x320 像素的阈值图像。我通过设置 ROI 以 20x20 像素的 block 循环遍历整个图像。我需要找到每个 block 的平均值。所以我将这些图像 block 传递给函数
我正在尝试学习 Javascript。我已经构建了以下代码来从一组数字中找到平均值。它有效除了最后返回的值总是 NaN。我不知道为什么。如果我将这 block 移到 block 外,它似乎完全忘记了变
假设我的数据已经分组,我该如何计算中位数和其他统计数据? Index Value Count 0 6 2 1 2 3 2 9 8 在上面
我试图计算的有趣情况。基本上在一行中,我有产品名称,其右侧的行是自首次收到产品以来经过的天数。 为 ex 计算的天数是 =TODAY()-BB2 我现在要做的是识别让我们说产品词“卡车”,然后计算卡车
我想知道如何计算某些数字的累积平均值。我将举一个简单的例子来描述我在寻找什么。 我有以下号码 vec 1) 为您的向量(或列表、一维数组或您如何称呼它)的每个元素评估此表达式,您将获得累积平均值。
我正在尝试对数据库表中的每一行进行平均。但它不能正常工作我想忽略该值,如果为空,它不会计算为零。使用我的代码,它将空值计算为零我想这样做 MS Excel 如果行/单元格为空,它将忽略。 Contro
我有以下信息(按 View 返回): DateTime ItemID UserTyp Seconds 2012-01-01 10 S 12 2012-01-01
我正在使用excel的average函数来获取欧洲各个城市一系列酒店价格的平均值。 =average(21,42,63,84,105) 我希望能够计算每个平均函数中的变量数量(例如,在上面的示例中有
我有一长串列,我想一次性计算非零中位数、平均值和标准差。我不能只删除基于 1 列的 0 行,因为同一列中另一列的值可能不是 0。 下面是我目前的代码,用于计算中位数、平均值等,包括零。 agg
这是我的问题: 我有一张这样的 table : Table Log int id; int time; timestamp DATE; int sid (FK to table Site);
JSON: [{"id":"1","user":"001","answer":"1,1,3,2,2,1,3,2"}, {"id":"2","user":"002","answer":"2,3,3,2,
有个问题: 使用适当的列名称,显示 obs 类型“CONT”的允许 ID 和平均 obs 值,其中 CONT 的平均 obs 值 >= 40。 假设承认是表1,观察是表2,但具有相同的主键Admit_
我有一个记录传感器数据的应用程序,我希望能够从多个传感器生成平均值,可以是一个、两个、三个或很多... 编辑:这些是温度传感器,因此 0 是传感器可能作为值存储在数据库中的值。 我最初的出发点是这个
我有这样一个数据框 id power flag 0 20 0 1 25 0 2 26 1 3 30 1 4 18 0 5
我想计算所有事件 blob 的平均位置。为此,首先我需要所有 X 和 Y 位置的总和。在这种情况下我该怎么做? contourFinder.findContours(grayImg, minB
我是一个十足的 Java 新手。上周一开始,之前从未用任何语言进行过任何编程。因此,如果我发现简单的事情变得复杂,请耐心等待。 我收到了一个文本文件。如下图: 第一个数据是时间(午夜过后的秒数),第二
我正在尝试为 Audacity 编写一个简单的测量插件,它就像用石头砸我的头骨一样有趣。我想要做的就是获取一段音频并找到所有样本的平均值(该 block 的 DC offset ),这样我就可以将它作
我正在尝试计算给定多边形内的值: 实际上我正在使用这个管道: 'aggregation': { 'pipeline': [ { "$match" : {
我有一个 pandas DataFrame,其中包含包含列表的列。我正在尝试获取此专栏中列表的方法。 这是我的 DataFrame 的示例: Loc Background 0
我尝试加速计算放置在数组中的4d向量的平均值。这是我的代码: #include #include #include #include #include #include typedef f
我是一名优秀的程序员,十分优秀!