- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题不太可能对任何 future 的访客有帮助;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于互联网的全局受众。如需帮助使这个问题更广泛适用,visit the help center .
8年前关闭。
使用来自 here 的信息我可以使用时间戳信息总结我的数据帧。
data <- read.csv("data.csv", header=T)
data$TIMESTAMP <- strptime(data$TIMESTAMP, "%m/%d/%Y %H:%M") # make unambigous
data$TIMESTAMP <- as.POSIXct(data$TIMESTAMP) # plyr/ddply does not seem to like POSIXlt
data$DAY <- as.factor(format(data$TIMESTAMP,'%d'))
data$MONTH <- as.factor(format(data$TIMESTAMP,'%m'))
ddply(data,.(MONTH,DAY),summarise, V1 = mean(P), V2 = max(WS)) # summarize by month by day
as.POSIXct
之前这样做,我可以对数据帧进行子集化(例如,创建一个新的数据帧,它是原始 df 的子集)行被执行。但是,当我尝试以下顺序时:
data.1.4 <- data[format(data$TIMESTAMP, "%H") >= 13 & format(data$TIMESTAMP, "%H") < 17,] # subset the data
data.1.4$TIMESTAMP <- as.POSIXct(data.1.4$TIMESTAMP)
data.1.4$DAY <- as.factor(format(data.1.4$TIMESTAMP,'%d'))
data.1.4$MONTH <- as.factor(format(data.1.4$TIMESTAMP,'%m'))
ddply(data.1.4,.(MONTH,DAY),summarise, V1 = mean(P), V2 = max(WS)) # summarize by month by day
Error in eval(expr, envir, enclos) : object 'MONTH' not found
.我能看到的唯一区别
data
和
data.1.4
是 row.names 是“可见的”和非连续的
data.1.4
数据框。
# dput of subset of data
data <- structure(list(TIMESTAMP = structure(1:15, .Label = c("1/1/2012 11:00",
"1/1/2012 12:00", "1/1/2012 13:00", "1/1/2012 14:00", "1/1/2012 15:00",
"1/2/2012 11:00", "1/2/2012 12:00", "1/2/2012 13:00", "1/2/2012 14:00",
"1/2/2012 15:00", "4/7/2012 11:00", "4/7/2012 12:00", "4/7/2012 13:00",
"4/7/2012 14:00", "4/7/2012 15:00"), class = "factor"), P = c(992.4,
992.4, 992.4, 992.4, 992.4, 992.4, 992.4, 992.4, 992.4, 992.4,
239, 239, 239, 239, 239), WS = c(4.023, 3.576, 4.023, 6.259,
4.47, 3.576, 3.576, 2.682, 4.023, 3.576, 2.682, 3.129, 2.682,
2.235, 2.682), WD = c(212L, 200L, 215L, 213L, 204L, 304L, 276L,
273L, 307L, 270L, 54L, 24L, 304L, 320L, 321L), AT = c(16.11,
18.89, 20, 20, 19.44, 10.56, 11.11, 11.67, 12.22, 11.11, 17.22,
18.33, 19.44, 20.56, 21.11), FT = c(17.22, 22.22, 22.78, 22.78,
20, 11.11, 15.56, 17.22, 17.78, 15.56, 24.44, 25.56, 29.44, 30.56,
29.44), H = c(50L, 38L, 38L, 39L, 48L, 24L, 19L, 18L, 16L, 18L,
23L, 20L, 18L, 17L, 15L), B = c(1029L, 1027L, 1026L, 1024L, 1023L,
1026L, 1025L, 1024L, 1023L, 1023L, 1034L, 1033L, 1032L, 1031L,
1030L), FM = c(14.9, 14.4, 14, 13.7, 13.6, 13.1, 12.8, 12.3,
12, 11.7, 12.8, 12, 11.4, 10.9, 10.4), GD = c(204L, 220L, 227L,
222L, 216L, 338L, 311L, 326L, 310L, 273L, 62L, 13L, 312L, 272L,
281L), MG = c(8.047, 9.835, 10.28, 13.41, 11.18, 9.388, 8.941,
8.494, 9.835, 10.73, 6.706, 7.153, 8.047, 8.047, 7.6), SR = c(522L,
603L, 604L, 526L, 248L, 569L, 653L, 671L, 616L, 487L, 972L, 1053L,
1061L, 1002L, 865L), WS2 = c(2.235, 3.576, 4.47, 4.47, 5.364,
4.023, 2.682, 3.576, 3.576, 4.023, 3.129, 3.129, 3.576, 2.682,
3.129), WD2 = c(200L, 201L, 206L, 210L, 211L, 319L, 315L, 311L,
302L, 290L, 49L, 39L, 15L, 348L, 329L)), .Names = c("TIMESTAMP",
"P", "WS", "WD", "AT", "FT", "H", "B", "FM", "GD", "MG", "SR",
"WS2", "WD2"), class = "data.frame", row.names = c(NA, -15L))
最佳答案
无趣的是,这里有一个您可能要考虑使用的解决方案 data.table
.如果您想取所有列的平均值,其中 hour >= 13
的代码甚至更小。和 hour <= 15
# changing the name from `TIMESTAMP` to `ts` : too long for me!
names(data)[1] <- "ts"
Step 1:
获取
data.table
并转换
ts
至
POSIXct
require(data.table)
dt <- data.table(data) # convert to data.table
dt[, ts := as.POSIXct(ts, format="%m/%d/%Y %H:%M")] # convert to POSIXct
Step 2:
获取
hour
,
day
和
month
# get hour, day, month
dt[, `:=`(day=as.POSIXlt(ts)$mday + 1,
month=as.POSIXlt(ts)$mon + 1,
hour=as.POSIXlt(ts)$hour)]
# set keys for blazing fast access
setkey(dt, "day","month")
step 3:
获取所有必要的平均值(一行代码)
# the `by` part groups your data.table by day and month
# The group can be accessed by `.SD` and we filter there for hours
# then we select all the columns that we want using `with=FALSE` that makes
# data.table behave like a data.frame only for that operation
# then we use `lapply` to take the mean of all the columns.
# all this can be done in one line! That's the sweetest part!
dt[, lapply(.SD[hour >= 13 & hour <= 15][, 2:13, with=FALSE], mean), by = "day,month"]
# day month P WS WD AT FT H B FM GD MG SR WS2
# 1: 2 1 992.4 4.917333 210.6667 19.81333 21.85333 41.66667 1024.333 13.76667 221.6667 11.623333 459.3333 4.768
# 2: 3 1 992.4 3.427000 283.3333 11.66667 16.85333 17.33333 1023.333 12.00000 303.0000 9.686333 591.3333 3.725
# 3: 8 4 239.0 2.533000 315.0000 20.37000 29.81333 16.66667 1031.000 10.90000 288.3333 7.898000 976.0000 3.129
subset
为
hour
然后得到
mean
通过选择
columns
:
dt1 <- dt[, .SD[hour >= 13 & hour <= 15], by="day,month"]
dt1[, lapply(.SD, mean), by="day,month", .SDcols=4:17]
关于r - 在 ddply 汇总之前从数据框中选择范围(时间戳),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14617945/
我不能解决这个问题。和标题说的差不多…… 如果其他两个范围/列中有“否”,我如何获得范围或列的平均值? 换句话说,我想计算 A 列的平均值,并且我有两列询问是/否问题(B 列和 C 列)。我只希望 B
我知道 python 2to3 将所有 xrange 更改为 range 我没有发现任何问题。我的问题是关于它如何将 range(...) 更改为 list(range(...)) :它是愚蠢的,只是
我有一个 Primefaces JSF 项目,并且我的 Bean 注释有以下内容: @Named("reportTabBean") @SessionScoped public class Report
在 rails3 中,我在模型中制作了相同的范围。例如 class Common ?" , at) } end 我想将公共(public)范围拆分为 lib 中的模块。所以我试试这个。 module
我需要在另一个 View 范围 bean 中使用保存在 View 范围 bean 中的一些数据。 @ManagedBean @ViewScoped public class Attivita impl
为什么下面的代码输出4?谁能给我推荐一篇好文章来深入学习 javascript 范围。 这段代码返回4,但我不明白为什么? (function f(){ return f(); functio
我有一个与此结构类似的脚本 $(function(){ var someVariable; function doSomething(){ //here } $('#som
我刚刚开始学习 Jquery,但这些示例对我帮助不大...... 现在,以下代码发生的情况是,我有 4 个表单,我使用每个表单的链接在它们之间进行切换。但我不知道如何在第一个函数中获取变量“postO
为什么当我这样做时: function Dog(){ this.firstName = 'scrappy'; } Dog.firstName 未定义? 但是我可以这样做: Dog.firstNa
我想打印文本文件 text.txt 的选定部分,其中包含: tickme 1.1(no.3) lesson1-bases lesson2-advancedfurther para:using the
我正在编写一些 JavaScript 代码。我对这个关键字有点困惑。如何在 dataReceivedHandler 函数中访问 logger 变量? MyClass: { logger: nu
我有这个代码: Public Sub test() Dim Tgt As Range Set Tgt = Range("A1") End Sub 我想更改当前为“A1”的 Tgt 的引
我正忙于此工作,以为我会把它放在我们那里。 该数字必须是最多3个单位和最多5个小数位的数字,等等。 有效的 999.99999 99.9 9 0.99999 0 无效的 -0.1 999.123456
覆盖代码时: @Override public void open(ExecutionContext executionContext) { super.open(executio
我想使用 preg_match 来匹配数字 1 - 21。我如何使用 preg_match 来做到这一点?如果数字大于 21,我不想匹配任何东西。 example preg_match('([0-9]
根据docs range函数有四种形式: (range) 0 - 无穷大 (range end) 0 - 结束 (range start end)开始 - 结束 (range start end st
我知道有一个UISlider,但是有人已经制作了RangeSlider(用两个拇指吗?)或者知道如何扩展 uislider? 最佳答案 我认为你不能直接扩展 UISlider,你可能需要扩展 UICo
我正在尝试将范围转换为列表。 nums = [] for x in range (9000, 9004): nums.append(x) print nums 输出 [9000] [9
请注意:此问题是由于在运行我的修饰方法时使用了GraphQL解析器。这意味着this的范围为undefined。但是,该问题的基础知识对于装饰者遇到问题的任何人都是有用的。 这是我想使用的基本装饰器(
我正在尝试创建一个工具来从网页上抓取信息(是的,我有权限)。 到目前为止,我一直在使用 Node.js 结合 requests 和 Cheerio 来拉取页面,然后根据 CSS 选择器查找信息。我已经
我是一名优秀的程序员,十分优秀!