如何在不同长度的时间序列中创建随机间隙?

问题描述

对于我的硕士学位,我必须检查现有数据集上的不同填充方法。因此,我必须添加不同长度的人工间隙(1h,5h ..),以便可以用不同的方法填充它们。有简单的功能吗?

这是数据框的示例:

class ApplePay: NSObject { 
 
var applePayItem: PKPaymentSummaryItem?
var baseVC: UIViewController?
let payNetworks = [PKPaymentNetwork.masterCard,.visa,.amex,.discover]


init(forItem: Product) {
    applePayItem = PKPaymentSummaryItem.init(label: forItem.name ?? "",amount: NSDecimalNumber(value: forItem.price ?? 0))
}

func initiatePayment(complete: @escaping (Bool) -> Void) {
    if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: payNetworks) {
        let request = PKPaymentRequest()
            request.currencyCode = "USD"
            request.countryCode = "US"
            request.merchantIdentifier = <*identifier from my development account*>
            request.merchantCapabilities = PKMerchantCapability.capability3DS
            request.supportednetworks = payNetworks
            request.paymentSummaryItems = [applePayItem!]
        guard let paymentVC = PKPaymentAuthorizationViewController(paymentRequest: request) else {
            return
        }
        baseVC = UIApplication.shared.keyWindow?.rootViewController?.children.last
        baseVC?.present(paymentVC,animated: true,completion: nil)
        paymentVC.delegate = self
        complete(true)
    } else {
        complete(false)
        CaAssembly.resolve(CaAlertProtocol.self)!.showAlert(CaConstant.alertTitles.addCardApplePay,dismiss: {})
    }
}

func paymentAuthorizationControllerDidFinish(_ controller: PKPaymentAuthorizationController) {
    controller.dismiss {
        
}
func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
    controller.dismiss(animated: true,completion: nil)
}



 }

 extension ApplePay: PKPaymentAuthorizationViewControllerDelegate,PKPaymentAuthorizationControllerDelegate {
 @available(iOS 11.0,*)
 func paymentAuthorizationViewController(_ controller: 
   PKPaymentAuthorizationViewController,didAuthorizePayment payment: PKPayment,handler completion: @escaping 
  (PKPaymentAuthorizationResult) -> Void) {
    print(“PKPaymentAuthorizationViewController didAuthorizePayment” )
   }
}

解决方法

如果我正确理解了您的问题,则可能的解决方法是:

set.seed(4) # make it reproducable

del <- sort(sample(1:nrow(df),4,replace=FALSE)) # get 4 random indexex from the total number of rows and sort them

del2 <-  del[diff(del) !=1] # delete those values that have a difference of 1 (meaning "connected")

df[del2,c(2:5)] <- NA # set column 2 to 5 NA for the indices we calculated above

   DateTime             `Dd 1-1` `Dd 1-3` `Dd 1-5`   luecken
   <dttm>                  <dbl>    <dbl>    <dbl>     <dbl>
 1 2015-01-01 01:00:00  0.0186    0.0243    0.0213  0.0186  
 2 2015-01-01 02:00:00  0.0243    0.0349    0.0284  0.0243  
 3 2015-01-01 03:00:00 NA        NA        NA      NA       
 4 2015-01-01 04:00:00  0.000967  0.00136   0.0143  0.000967
 5 2015-01-01 05:00:00  0.0119    0.0221    0.0276  0.0119  
 6 2015-01-01 06:00:00  0.0496    0.0601    0.0329  0.0496  
 7 2015-01-01 07:00:00  0.0201    0.0462    0.0495  0.0201  
 8 2015-01-01 08:00:00  0.0307    0.0172    0.0173  0.0307  
 9 2015-01-01 09:00:00 NA        NA        NA      NA       
10 2015-01-01 10:00:00  0.0192    0.0227    0.0177  0.0192 

请明确一点:清理连接间隙的步骤并不完全正确,因为随机数为1-4会掉2、3和4,但是在大数据上,如果您选择与整个数据集相比,我们不打算放弃很多值

现在介绍如何创建更大的差距(我将使用3h,因为您的示例数据只有10行)

set.seed(4)

del <- sort(sample(1:nrow(df),3,replace=FALSE))

del2 <- del[diff(del) > 3] #set difference to more than maximum size of gap wanted

del3 <- c(del2,del2 + 1,del2 + 2) # build vector with +1 and +2 to get indices conecting conecting to the onces you have

del4 <- del3[del3 <= nrow(df)] # make sure it is not out of bound (max index should be 10 even if gap starts at line 10

df[del4,c(2:5)] <- NA

    DateTime            `Dd 1-1` `Dd 1-3` `Dd 1-5` luecken
   <dttm>                 <dbl>    <dbl>    <dbl>   <dbl>
 1 2015-01-01 01:00:00   0.0186   0.0243   0.0213  0.0186
 2 2015-01-01 02:00:00   0.0243   0.0349   0.0284  0.0243
 3 2015-01-01 03:00:00  NA       NA       NA      NA     
 4 2015-01-01 04:00:00  NA       NA       NA      NA     
 5 2015-01-01 05:00:00  NA       NA       NA      NA     
 6 2015-01-01 06:00:00   0.0496   0.0601   0.0329  0.0496
 7 2015-01-01 07:00:00   0.0201   0.0462   0.0495  0.0201
 8 2015-01-01 08:00:00   0.0307   0.0172   0.0173  0.0307
 9 2015-01-01 09:00:00  NA       NA       NA      NA     
10 2015-01-01 10:00:00  NA       NA       NA      NA