简体   繁体   English

使用R基于TimeSeries数据添加行

[英]Adding Rows based on TimeSeries Data using R

Consider the following DataSet; 考虑以下数据集;

scd <- read.table(text = "
2019-04-01 10:00:00 | 2019-04-01 12:00:00 | 10
2019-04-02 10:00:00 | 2019-04-02 12:00:00 | 5
2019-04-03 13:00:00 | 2019-04-03 15:00:00 | 7
2019-04-04 16:00:00 | 2019-04-04 19:00:00 | 5
2019-04-05 10:00:00 | 2019-04-05 12:00:00 | 6
2019-04-06 10:00:00 | 2019-04-06 12:00:00 | 5", sep = "|")

colnames(scd) <- c('start_date_ts', 'end_date_ts', 'people_count')

The above code consists of start date and end date with time, with the assumption that for each hour, I can expect a count increase mentioned in the people count column. 上面的代码由开始日期和结束日期以及时间组成,并假设我希望每小时在人员计数列中提到的计数增加。

For Example, take Row 1, it says that from 10 AM to 12PM, I can expect count to increase by 10. 例如,以第1行为例,它说从上午10点到下午12点,我预计计数会增加10。

2019-04-01 10:00:00 = 10 + Actual Data 2019-04-01 10:00:00 = 10 +实际数据

2019-04-01 11:00:00 = 10 + Actual Data 2019-04-01 11:00:00 = 10 +实际数据

2019-04-01 12:00:00 = 10 + Actual Data 2019-04-01 12:00:00 = 10 +实际数据

Actual Data; 实际数据;

fc_data <- read.table(text = "
2019-04-01 10:00:00 | 10
2019-04-01 12:00:00 | 5
2019-04-04 19:00:00 | 5
2019-04-05 12:00:00 | 6
2019-04-06 08:00:00 | 3", sep = "|")

colnames(fc_data) <- c('pred_t', 'fpc')

I am expecting the following outcome; 我期待以下结果; (from the fc_data) (来自fc_data)

Row 1 - 10 + 10 = 20 第1-10 + 10 = 20行

Row 2 - 5 + 10 = 15 第2-5 + 10 = 15行

Row 3 - 5 + 5 = 10 第3-5 + 5 = 10行

Row 4 - 6 + 6 = 12 第4-6 + 6行= 12

Row 5 - 3 + 0 = 3 第5-3 + 0 = 3行

I want the code to run through each row and match with the start and end time and provide me with the output provided above. 我希望代码遍历每一行并与开始时间和结束时间匹配,并为我提供上面提供的输出。

My Approach; 我的方法

fc_data$events_pc <- with(fc_data, ifelse(fc_data$pred_t == scd$start_date_ts | fc_data$pred_t == scd$end_date_ts &
                                        fc_data$pred_t == scd$end_date_ts,
                                      fc_data$fpc + scd$people_count, fc_data$fpc + 0))

Although, I get some of the rows added up, the others actually don't match up. 尽管我将一些行加起来,但其他行实际上不匹配。 I have searched the Stack for some information, but, I am unable to find any. 我已经在堆栈中搜索了一些信息,但是找不到任何信息。 Any inputs will be very helpful. 任何输入将非常有帮助。

We can use mapply and match the start_date_ts and end_date_ts from scd with pred_t , get the corresponding people_count and add it to fpc . 我们可以使用mapply并将scd中的start_date_tsend_date_tspred_t ,获得相应的people_count并将其添加到fpc

mapply(function(x, y) {
   inds <- x >= scd$start_date_ts & x <= scd$end_date_ts
   if (any(inds))  
      y + scd$people_count[inds]
   else
      y
}, fc_data$pred_t, fc_data$fpc)

#[1] 20 15 10 12  3

Make sure the date-time variable are in POSIXct format, if they are not you need to change them. 确保date-time变量为POSIXct格式,如果不是,则需要更改它们。

fc_data$pred_t <- as.POSIXct(fc_data$pred_t)
scd[1:2] <- lapply(scd[1:2], as.POSIXct)

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

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