繁体   English   中英

R如何:以第二分辨率读取CSV,绘图速率

[英]R How To: Read CSV, plot rate in second resolution

假设一组简单但大量的数据(8小时1或2秒分辨率为14k-28k)

  1. 如何从csv读取并创建一个绘图,其中多个比率绘制在y轴上,时间在x轴上。
  2. 如何以15分钟或30分钟的间隔汇总x轴。
  3. 如何过滤集合,以便我可以放大数据的特定部分
  4. 如何计算先前x值(即10秒)的平均值,以便在ValueB中有效地忽略非常低的值

作为30多年用多种语言编程的人,在R中使用日期似乎非常困难。我在帖子中看到的问题和答案似乎对解决方案非常具体(即成为整个包中的主人)很难在其他地方申请。 我错过了什么? 这感觉它应该是愚蠢的简单但不是。 我已经看到了上述所有问题的部分答案,但将它们放在一起似乎并不可行。

我应该阅读哪些关键概念和术语?

time,ValueA,ValueB
3/12/2014 11:12:14,15222,3882
3/12/2014 11:12:16,5462,9832
3/12/2014 11:12:18,8432,12281
3/12/2014 11:12:20,15325,19928
3/12/2014 11:12:22,17458,29382
3/12/2014 11:12:24,6541,12
3/12/2014 11:12:26,8287,17822
3/12/2014 11:12:28,14278,504
3/12/2014 11:12:30,11854,848
3/12/2014 11:12:32,7495,17899
3/12/2014 11:12:34,6387,38822
3/12/2014 11:12:36,12354,7732
3/12/2014 11:12:38,15422,2003
3/12/2014 11:12:40,8452,2
3/12/2014 11:12:42,5845,18388

我在这里给你所有问题的完整答案。 我同意使用日期并不是很容易(我认为,任何语言,时区头条,日期转换,...​​...)。 您肯定在寻找xts/zoo软件包。 这是时间序列专业包。 非常快速,高效和良好使用。 当然你可以在基础R中做,但是一旦掌握了xts包,它就会更容易。

阅读情节数据:

library(xts)
## you reaplce text = by filename= and you give your file
dts <- read.zoo(text='time,ValueA,ValueB
3/12/2014 11:12:14,15222,3882
3/12/2014 11:12:16,5462,9832
3/12/2014 11:12:18,8432,12281
3/12/2014 11:12:20,15325,19928
3/12/2014 11:12:22,17458,29382
3/12/2014 11:12:24,6541,12
3/12/2014 11:12:26,8287,17822
3/12/2014 11:12:28,14278,504
3/12/2014 11:12:30,11854,848
3/12/2014 11:12:32,7495,17899
3/12/2014 11:12:34,6387,38822
3/12/2014 11:12:36,12354,7732
3/12/2014 11:12:38,15422,2003
3/12/2014 11:12:40,8452,2
3/12/2014 11:12:42,5845,18388',header=TRUE,tz='',
         sep=',',format='%d/%m/%Y %H:%M:%S')
myColors <- c("red", "darkgreen")
plot(x = dts, xlab = "Time", ylab = "Value",
     main = "plot rate in second resolution", col = myColors, screens = 1)
legend(x = "topleft", legend = c("ValueA", "ValueB"),
       lty = 1, col = myColors])

在此输入图像描述

如何总结x轴

您应该使用period.applyendpoints来创建时间间隔。 在这里,我展示了它几秒钟的单位,但在你的cas中你应该使用mins

period.apply(dts,endpoints(dts,'seconds',k=15),mean)
                      ValueA ValueB
2014-12-03 11:12:14 15222.00   3882
2014-12-03 11:12:28 10826.14  12823
2014-12-03 11:12:42  9687.00  12242

缩放数据:

您可以使用xts具有时间子集

dts <- as.xts(dts)
dts['T11:12:16/T11:12:19']

                    ValueA ValueB
2014-12-03 11:12:16   5462   9832
2014-12-03 11:12:18   8432  12281

如何计算先前x值的平均值

rollmean(dts,k=10)
                     ValueA  ValueB
2014-12-03 11:12:22 11035.4 11239.0
2014-12-03 11:12:24 10151.9 14733.0
2014-12-03 11:12:26 10841.1 14523.0
2014-12-03 11:12:28 11540.1 13495.2
2014-12-03 11:12:30 10852.8 11502.6
2014-12-03 11:12:32  9691.5 10403.2

1.如何从csv读取并创建一个图表,其中多个比率绘制在y轴上,时间在x轴上。

 > dat <- read.csv(header = TRUE, text = "time,ValueA,ValueB 3/12/2014 11:12:14,15222,3882 3/12/2014 11:12:16,5462,9832 3/12/2014 11:12:18,8432,12281 3/12/2014 11:12:20,15325,19928 ... ", sep = ",") > dat$time <- strptime(dat$time, format = "%m/%d/%Y %H:%M:%S") > sapply(dat, class) ## $time ## [1] "POSIXlt" "POSIXt" ## $ValueA ## [1] "integer" ## $ValueB ## [1] "integer" 

@ agstudy的情节非常好。

2.如何以15分钟或30分钟的间隔汇总x轴。

在这里,我会做几秒钟,因为数据已经以这种方式设置。

 > spl <- split(dat , cut(dat$time, "15 secs")) > spl ## $`2014-03-12 11:12:14` ## time ValueA ValueB ## 1 2014-03-12 11:12:14 15222 3882 ## 2 2014-03-12 11:12:16 5462 9832 ## 3 2014-03-12 11:12:18 8432 12281 ## 4 2014-03-12 11:12:20 15325 19928 ## 5 2014-03-12 11:12:22 17458 29382 ## 6 2014-03-12 11:12:24 6541 12 ## 7 2014-03-12 11:12:26 8287 17822 ## 8 2014-03-12 11:12:28 14278 504 ## $`2014-03-12 11:12:29` ## time ValueA ValueB ## 9 2014-03-12 11:12:30 11854 848 ## 10 2014-03-12 11:12:32 7495 17899 ## 11 2014-03-12 11:12:34 6387 38822 ## 12 2014-03-12 11:12:36 12354 7732 ## 13 2014-03-12 11:12:38 15422 2003 ## 14 2014-03-12 11:12:40 8452 2 ## 15 2014-03-12 11:12:42 5845 18388 

3.如何过滤集合,以便我可以放大数据的特定部分

例如,获取ValueA值大于10,000。

> dat[dat$ValueA > 1e4, ]

##                   time ValueA ValueB
## 1  2014-03-12 11:12:14  15222   3882
## 4  2014-03-12 11:12:20  15325  19928
## 5  2014-03-12 11:12:22  17458  29382
## 8  2014-03-12 11:12:28  14278    504
## 9  2014-03-12 11:12:30  11854    848
## 12 2014-03-12 11:12:36  12354   7732
## 13 2014-03-12 11:12:38  15422   2003

4.如何计算先前x值(即10秒)的平均值,以便在ValueB中有效地忽略非常低的值

将数据拆分为10秒间隔并找到有价值列的mean

 spl <- split(dat , cut(dat$time, "10 secs")) do.call(rbind, lapply(1:length(spl), function(i){ A <- mean(spl[[i]]$ValueA) B <- mean(spl[[i]]$ValueB) data.frame(A, B) })) ## AB ## 1 12379.8 15061.0 ## 2 9691.0 7417.0 ## 3 9692.0 13389.4 

我还建议阅读difftimeas.Date以及这些帮助文件中的所有链接函数。 对不起这么久! 希望能帮助到你。

首先定义数据的可重复性:

Lines <- "time,ValueA,ValueB
3/12/2014 11:12:14,15222,3882
3/12/2014 11:12:16,5462,9832
3/12/2014 11:12:18,8432,12281
3/12/2014 11:12:20,15325,19928
3/12/2014 11:12:22,17458,29382
3/12/2014 11:12:24,6541,12
3/12/2014 11:12:26,8287,17822
3/12/2014 11:12:28,14278,504
3/12/2014 11:12:30,11854,848
3/12/2014 11:12:32,7495,17899
3/12/2014 11:12:34,6387,38822
3/12/2014 11:12:36,12354,7732
3/12/2014 11:12:38,15422,2003
3/12/2014 11:12:40,8452,2
3/12/2014 11:12:42,5845,18388
"

现在我们执行4个步骤。

library(zoo)
library(ggplot2)

## 1 - Read in and create some plots.
## Use something like file="myfile.csv" in place of text=Lines on real data.

fmt <- "%m/%d/%Y %H:%M:%S"
z <- read.zoo(text = Lines, header = TRUE, sep = ",", format = fmt, tz = "")

plot(z)
plot(z, screen = 1, col = 1:2)

autoplot(z)
autoplot(z) + facet_free()
autoplot(z, facet = NULL)

## 2 - Aggregate to 15 sec.  cut produces factor so convert back to POSIXct.
## Use "15 min" instead on real data.

z15 <- aggregate(z, as.POSIXct(cut(time(z), "15 sec")), mean)

## 3 - Subset to a window of times.
## Modify st and en as desired for real data.

st <- as.POSIXct("2014-03-12 11:12:10")
en <- as.POSIXct("2014-03-12 11:12:40")
zw <- window(z, start = st, end = en)

## 4 - Average last k points.
## Use k <- 10 on real data

k <- 3
rollmeanr(z, k)
rollapplyr(z, k, by = k, mean) # or do this for every kth point

请注意,每个xts对象也是一个zoo对象,另一个答案已经给出了xts解决方案。 人们可以在动物园和xts之间来回走动。 例如x <- as.xts(z)

动物园附带5个小插图(pdf文档)以及帮助文件/参考手册。 点击这里的文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM