简体   繁体   English

有没有办法在 r 中使用 map 或 lapply 命令传递时间序列数据的子集?

[英]Is there a way to pass a subset of time series data with the map or lapply command in r?

To improve my R programming and simplify my code, I'm trying to replace a 'for' loop with 'lapply', 'map', or a similar variant.为了改进我的 R 编程并简化我的代码,我试图用“lapply”、“map”或类似的变体替换“for”循环。 I want to perform a function using 2 minute time intervals from my time series data.我想使用我的时间序列数据中的 2 分钟时间间隔执行 function。 When I try to pass just a subset of the data using one of the functionals, 'map' or 'lapply', I get a 'subscript out of bounds error'.当我尝试使用函数“map”或“lapply”之一仅传递数据的一个子集时,我得到一个“下标越界错误”。 Any ideas?有任何想法吗?

library(lubridate)
library(purrr)
library(stats)
library(xts)
# Set up the time series data
t <- ymd_hms("2020-01-01 08:00:00","2020-01-01 08:01:00","2020-01-01 08:02:00", "2020-01-01 08:03:00")
tsData <- as.xts(1:4,order.by=(t))
# Set up the summary time periods; in this case every 2 minutes
timeSlots <- seq(from=t[1],to=t[length(t)],by=120)
# Make sure it also summarizes the last period
lastTime <- stats::time(tsData[nrow(tsData)])

# The next 4 lines iterate through the time series and print a summary for each 2 minute time period; 
# This is the loop I want to replace with 'map'
for (i in 1:(length(timeSlots))) {
  if (i < length(timeSlots)) {
    print (summary (tsData[paste(timeSlots[i],'/',(timeSlots[i+1]-1),sep='')]))
  }
# This makes sure the last subset includes the last observation
  else print (summary (tsData[paste(timeSlots[i],'/',lastTime,sep='')]))
}

# This next statement gets a subscript out of bounds error
lapply (timeSlots, function(x) summary(tsData[x:x+1]))

# This next statement gets a subscript out of bounds error
map (timeSlots,function(x) summary(tsData[x:x+1]))

We can loop over the sequence and paste as in the for loop我们可以像在for循环中一样遍历序列并paste

library(xts)
lapply(seq_along(timeSlots), function(i) 
   if(i < length(timeSlots)) {
    summary(tsData[paste(timeSlots[i], timeSlots[i+1]-1, sep="/")])

   } else {
       summary (tsData[paste(timeSlots[i],'/',lastTime,sep='')])
       }

    )

#[[1]]
#     Index                     tsData[paste(timeSlots[i], timeSlots[i + 1] - 1, sep = "/")]
# Min.   :2020-01-01 08:00:00   Min.   :1.00                                                
# 1st Qu.:2020-01-01 08:00:15   1st Qu.:1.25                                                
# Median :2020-01-01 08:00:30   Median :1.50                                                
# Mean   :2020-01-01 08:00:30   Mean   :1.50                                                
# 3rd Qu.:2020-01-01 08:00:45   3rd Qu.:1.75                                                
# Max.   :2020-01-01 08:01:00   Max.   :2.00                                                

#[[2]]
#     Index                     tsData[paste(timeSlots[i], "/", lastTime, sep = "")]
# Min.   :2020-01-01 08:02:00   Min.   :3.00                                        
# 1st Qu.:2020-01-01 08:02:15   1st Qu.:3.25                                        
# Median :2020-01-01 08:02:30   Median :3.50                                        
# Mean   :2020-01-01 08:02:30   Mean   :3.50                                        
# 3rd Qu.:2020-01-01 08:02:45   3rd Qu.:3.75                                        
# Max.   :2020-01-01 08:03:00   Max.   :4.00  

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

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