2012-06-27 78 views
1

我具有介於5分鐘的價格觀測(未設置XTS)的數據集如下:如何跳過連續的行觀察?

dat = read.csv("olddat.csv", as.is=TRUE); 
tim = strptime(dat[,1], "%d/%m/%Y %H:%M"); 
dat$day = format(tim, "%Y-%m-%d"); 
##ddx <- row index for each day 
ddx = split(seq.int(nrow(dat)), dat$day); 

返回:(這是數據的只有一天,我來自R複製在和上觀測每一天通常進行,直到23點55分,整個集有更多的天數)的數據被下載時

2423 24/11/2011 00:00 1.3356 2011-11-24 
2424 24/11/2011 00:05 1.3356 2011-11-24 
2425 24/11/2011 00:10 1.3356 2011-11-24 
2426 24/11/2011 00:15 1.3359 2011-11-24 
2427 24/11/2011 00:20 1.3363 2011-11-24 
2428 24/11/2011 00:25 1.3364 2011-11-24 
2429 24/11/2011 00:30 1.3364 2011-11-24 
2430 24/11/2011 00:35 1.3362 2011-11-24 
2431 24/11/2011 00:40 1.3362 2011-11-24 
2432 24/11/2011 00:45 1.3361 2011-11-24 
2433 24/11/2011 00:50 1.3357 2011-11-24 
2434 24/11/2011 00:55 1.3361 2011-11-24 
2435 24/11/2011 01:00 1.3365 2011-11-24 
2436 24/11/2011 01:05 1.3367 2011-11-24 
2437 24/11/2011 01:10 1.3368 2011-11-24 
2438 24/11/2011 01:15 1.3365 2011-11-24 
2439 24/11/2011 01:20 1.3364 2011-11-24 
2440 24/11/2011 01:25 1.3366 2011-11-24 
2441 24/11/2011 01:30 1.3369 2011-11-24 
2442 24/11/2011 01:35 1.3369 2011-11-24 
2443 24/11/2011 01:40 1.3369 2011-11-24 
2444 24/11/2011 01:45 1.3367 2011-11-24 
2445 24/11/2011 01:50 1.3368 2011-11-24 
2446 24/11/2011 01:55 1.3367 2011-11-24 
2447 24/11/2011 02:00 1.3367 2011-11-24 
2448 24/11/2011 02:05 1.3365 2011-11-24 
2449 24/11/2011 02:10 1.3365 2011-11-24 
2450 24/11/2011 02:15 1.3372 2011-11-24 
2451 24/11/2011 02:20 1.3373 2011-11-24 
2452 24/11/2011 02:25 1.3376 2011-11-24 
2453 24/11/2011 02:30 1.3376 2011-11-24 
2454 24/11/2011 02:35 1.3379 2011-11-24 
2455 24/11/2011 02:40 1.3379 2011-11-24 
2456 24/11/2011 02:45 1.3378 2011-11-24 
2457 24/11/2011 02:50 1.3378 2011-11-24 
2458 24/11/2011 02:55 1.3377 2011-11-24 
2459 24/11/2011 03:00 1.3377 2011-11-24 
2460 24/11/2011 03:05 1.3377 2011-11-24 
2461 24/11/2011 03:10 1.3379 2011-11-24 
2462 24/11/2011 03:15 1.3384 2011-11-24 
2463 24/11/2011 03:20 1.3383 2011-11-24 
2464 24/11/2011 03:25 1.3382 2011-11-24 
2465 24/11/2011 03:30 1.3382 2011-11-24 
2466 24/11/2011 03:35 1.3383 2011-11-24 
2467 24/11/2011 03:40 1.3383 2011-11-24 
2468 24/11/2011 03:45 1.3381 2011-11-24 
2469 24/11/2011 03:50 1.3380 2011-11-24 
2470 24/11/2011 03:55 1.3381 2011-11-24 
2471 24/11/2011 04:00 1.3383 2011-11-24 
2472 24/11/2011 04:05 1.3381 2011-11-24 
2473 24/11/2011 04:10 1.3382 2011-11-24 
2474 24/11/2011 04:15 1.3381 2011-11-24 
2475 24/11/2011 04:20 1.3379 2011-11-24 
2476 24/11/2011 04:25 1.3379 2011-11-24 
2477 24/11/2011 04:30 1.3379 2011-11-24 
2478 24/11/2011 04:35 1.3373 2011-11-24 
2479 24/11/2011 04:40 1.3373 2011-11-24 
2480 24/11/2011 04:45 1.3371 2011-11-24 
2481 24/11/2011 04:50 1.3368 2011-11-24 
2482 24/11/2011 04:55 1.3369 2011-11-24 
2483 24/11/2011 05:00 1.3373 2011-11-24 
2484 24/11/2011 05:05 1.3373 2011-11-24 
2485 24/11/2011 05:10 1.3372 2011-11-24 
2486 24/11/2011 05:15 1.3373 2011-11-24 
2487 24/11/2011 05:20 1.3376 2011-11-24 
2488 24/11/2011 05:25 1.3376 2011-11-24 
2489 24/11/2011 05:30 1.3375 2011-11-24 
2490 24/11/2011 05:35 1.3374 2011-11-24 
2491 24/11/2011 05:40 1.3371 2011-11-24 
2492 24/11/2011 05:45 1.3367 2011-11-24 
2493 24/11/2011 05:50 1.3364 2011-11-24 
2494 24/11/2011 05:55 1.3363 2011-11-24 
2495 24/11/2011 06:00 1.3361 2011-11-24 
2496 24/11/2011 06:05 1.3360 2011-11-24 
2497 24/11/2011 06:10 1.3362 2011-11-24 
2498 24/11/2011 06:15 1.3364 2011-11-24 
2499 24/11/2011 06:20 1.3362 2011-11-24 
2500 24/11/2011 06:25 1.3364 2011-11-24 
2501 24/11/2011 06:30 1.3366 2011-11-24 
2502 24/11/2011 06:35 1.3366 2011-11-24 
2503 24/11/2011 06:40 1.3366 2011-11-24 
2504 24/11/2011 06:45 1.3369 2011-11-24 
2505 24/11/2011 06:50 1.3377 2011-11-24 
2506 24/11/2011 06:55 1.3376 2011-11-24 
2507 24/11/2011 07:00 1.3388 2011-11-24 
2508 24/11/2011 07:05 1.3391 2011-11-24 
2509 24/11/2011 07:10 1.3394 2011-11-24 
2510 24/11/2011 07:15 1.3389 2011-11-24 
2511 24/11/2011 07:20 1.3390 2011-11-24 
2512 24/11/2011 07:25 1.3386 2011-11-24 
2513 24/11/2011 07:30 1.3387 2011-11-24 
2514 24/11/2011 07:35 1.3387 2011-11-24 
2515 24/11/2011 07:40 1.3392 2011-11-24 
2516 24/11/2011 07:45 1.3390 2011-11-24 
2517 24/11/2011 07:50 1.3392 2011-11-24 
2518 24/11/2011 07:55 1.3383 2011-11-24 
2519 24/11/2011 08:00 1.3378 2011-11-24 
2520 24/11/2011 08:05 1.3377 2011-11-24 
2521 24/11/2011 08:10 1.3377 2011-11-24 
2522 24/11/2011 08:15 1.3375 2011-11-24 
2523 24/11/2011 08:20 1.3373 2011-11-24 
2524 24/11/2011 08:25 1.3377 2011-11-24 
2525 24/11/2011 08:30 1.3380 2011-11-24 
2526 24/11/2011 08:35 1.3378 2011-11-24 
2527 24/11/2011 08:40 1.3378 2011-11-24 
2528 24/11/2011 08:45 1.3379 2011-11-24 
2529 24/11/2011 08:50 1.3378 2011-11-24 
2530 24/11/2011 08:55 1.3376 2011-11-24 
2531 24/11/2011 09:00 1.3394 2011-11-24 
2532 24/11/2011 09:05 1.3391 2011-11-24 
2533 24/11/2011 09:10 1.3402 2011-11-24 
2534 24/11/2011 09:15 1.3406 2011-11-24 
2535 24/11/2011 09:20 1.3399 2011-11-24 
2536 24/11/2011 09:25 1.3394 2011-11-24 
2537 24/11/2011 09:30 1.3397 2011-11-24 
2538 24/11/2011 09:35 1.3405 2011-11-24 
2539 24/11/2011 09:40 1.3412 2011-11-24 
2540 24/11/2011 09:45 1.3405 2011-11-24 
2541 24/11/2011 09:50 1.3403 2011-11-24 
2542 24/11/2011 09:55 1.3398 2011-11-24 
2543 24/11/2011 10:00 1.3402 2011-11-24 
2544 24/11/2011 10:05 1.3392 2011-11-24 
2545 24/11/2011 10:10 1.3384 2011-11-24 
2546 24/11/2011 10:15 1.3384 2011-11-24 
2547 24/11/2011 10:20 1.3375 2011-11-24 
2548 24/11/2011 10:25 1.3377 2011-11-24 
2549 24/11/2011 10:30 1.3377 2011-11-24 
2550 24/11/2011 10:35 1.3379 2011-11-24 
2551 24/11/2011 10:40 1.3378 2011-11-24 
2552 24/11/2011 10:45 1.3371 2011-11-24 
2553 24/11/2011 10:50 1.3373 2011-11-24 
2554 24/11/2011 10:55 1.3375 2011-11-24 
2555 24/11/2011 11:00 1.3372 2011-11-24 
2556 24/11/2011 11:05 1.3379 2011-11-24 
2557 24/11/2011 11:10 1.3383 2011-11-24 
2558 24/11/2011 11:15 1.3382 2011-11-24 
2559 24/11/2011 11:20 1.3382 2011-11-24 
2560 24/11/2011 11:25 1.3387 2011-11-24 
2561 24/11/2011 11:30 1.3387 2011-11-24 
2562 24/11/2011 11:35 1.3383 2011-11-24 
2563 24/11/2011 11:40 1.3383 2011-11-24 
2564 24/11/2011 11:45 1.3390 2011-11-24 
2565 24/11/2011 11:50 1.3391 2011-11-24 
2566 24/11/2011 11:55 1.3394 2011-11-24 
2567 24/11/2011 12:00 1.3409 2011-11-24 
2568 24/11/2011 12:05 1.3401 2011-11-24 
2569 24/11/2011 12:10 1.3401 2011-11-24 
2570 24/11/2011 12:15 1.3392 2011-11-24 
2571 24/11/2011 12:20 1.3388 2011-11-24 
2572 24/11/2011 12:25 1.3388 2011-11-24 
2573 24/11/2011 12:30 1.3383 2011-11-24 
2574 24/11/2011 12:35 1.3388 2011-11-24 
2575 24/11/2011 12:40 1.3393 2011-11-24 
2576 24/11/2011 12:45 1.3389 2011-11-24 
2577 24/11/2011 12:50 1.3382 2011-11-24 
2578 24/11/2011 12:55 1.3374 2011-11-24 
2579 24/11/2011 13:00 1.3374 2011-11-24 
2580 24/11/2011 13:05 1.3368 2011-11-24 
2581 24/11/2011 13:10 1.3373 2011-11-24 
2582 24/11/2011 13:15 1.3373 2011-11-24 
2583 24/11/2011 13:20 1.3378 2011-11-24 
2584 24/11/2011 13:25 1.3381 2011-11-24 
2585 24/11/2011 13:30 1.3386 2011-11-24 
2586 24/11/2011 13:35 1.3372 2011-11-24 
2587 24/11/2011 13:40 1.3361 2011-11-24 
2588 24/11/2011 13:45 1.3345 2011-11-24 
2589 24/11/2011 13:50 1.3338 2011-11-24 
2590 24/11/2011 13:55 1.3338 2011-11-24 
2591 24/11/2011 14:00 1.3346 2011-11-24 
2592 24/11/2011 14:05 1.3345 2011-11-24 
2593 24/11/2011 14:10 1.3343 2011-11-24 
2594 24/11/2011 14:15 1.3343 2011-11-24 
2595 24/11/2011 14:20 1.3342 2011-11-24 
2596 24/11/2011 14:25 1.3337 2011-11-24 
2597 24/11/2011 14:30 1.3329 2011-11-24 
2598 24/11/2011 14:35 1.3330 2011-11-24 
2599 24/11/2011 14:40 1.3329 2011-11-24 
2600 24/11/2011 14:45 1.3327 2011-11-24 
2601 24/11/2011 14:50 1.3333 2011-11-24 
2602 24/11/2011 14:55 1.3336 2011-11-24 
2603 24/11/2011 15:00 1.3334 2011-11-24 
2604 24/11/2011 15:05 1.3327 2011-11-24 
2605 24/11/2011 15:10 1.3330 2011-11-24 
2606 24/11/2011 15:15 1.3330 2011-11-24 
2607 24/11/2011 15:20 1.3331 2011-11-24 
2608 24/11/2011 15:25 1.3338 2011-11-24 
2609 24/11/2011 15:30 1.3338 2011-11-24 
2610 24/11/2011 15:35 1.3336 2011-11-24 
2611 24/11/2011 15:40 1.3330 2011-11-24 
2612 24/11/2011 15:45 1.3335 2011-11-24 
2613 24/11/2011 15:50 1.3335 2011-11-24 
2614 24/11/2011 15:55 1.3331 2011-11-24 
2615 24/11/2011 16:00 1.3329 2011-11-24 
2616 24/11/2011 16:05 1.3332 2011-11-24 
2617 24/11/2011 16:10 1.3330 2011-11-24 
2618 24/11/2011 16:15 1.3332 2011-11-24 
2619 24/11/2011 16:20 1.3335 2011-11-24 
2620 24/11/2011 16:25 1.3333 2011-11-24 
2621 24/11/2011 16:30 1.3330 2011-11-24 
2622 24/11/2011 16:35 1.3329 2011-11-24 
2623 24/11/2011 16:40 1.3333 2011-11-24 
2624 24/11/2011 16:45 1.3339 2011-11-24 
2625 24/11/2011 16:50 1.3337 2011-11-24 
2626 24/11/2011 16:55 1.3341 2011-11-24 
2627 24/11/2011 17:00 1.3344 2011-11-24 
2628 24/11/2011 17:05 1.3354 2011-11-24 
2629 24/11/2011 17:10 1.3353 2011-11-24 
2630 24/11/2011 17:15 1.3348 2011-11-24 
2631 24/11/2011 17:20 1.3345 2011-11-24 
2632 24/11/2011 17:25 1.3344 2011-11-24 
2633 24/11/2011 17:30 1.3345 2011-11-24 
2634 24/11/2011 17:35 1.3346 2011-11-24 
2635 24/11/2011 17:40 1.3345 2011-11-24 
2636 24/11/2011 17:45 1.3342 2011-11-24 
2637 24/11/2011 17:50 1.3338 2011-11-24 
2638 24/11/2011 17:55 1.3340 2011-11-24 
2639 24/11/2011 18:00 1.3341 2011-11-24 
2640 24/11/2011 18:05 1.3339 2011-11-24 
2641 24/11/2011 18:10 1.3339 2011-11-24 
2642 24/11/2011 18:15 1.3339 2011-11-24 
2643 24/11/2011 18:20 1.3342 2011-11-24 
2644 24/11/2011 18:25 1.3341 2011-11-24 
2645 24/11/2011 18:30 1.3341 2011-11-24 
2646 24/11/2011 18:35 1.3342 2011-11-24 
2647 24/11/2011 18:40 1.3342 2011-11-24 
2648 24/11/2011 18:45 1.3341 2011-11-24 
2649 24/11/2011 18:50 1.3343 2011-11-24 
2650 24/11/2011 18:55 1.3342 2011-11-24 
2651 24/11/2011 19:00 1.3342 2011-11-24 
2652 24/11/2011 19:05 1.3341 2011-11-24 
2653 24/11/2011 19:10 1.3340 2011-11-24 
2654 24/11/2011 19:15 1.3339 2011-11-24 
2655 24/11/2011 19:20 1.3338 2011-11-24 
2656 24/11/2011 19:25 1.3337 2011-11-24 
2657 24/11/2011 19:30 1.3336 2011-11-24 
2658 24/11/2011 19:35 1.3337 2011-11-24 
2659 24/11/2011 19:40 1.3337 2011-11-24 
2660 24/11/2011 19:45 1.3335 2011-11-24 
2661 24/11/2011 19:50 1.3338 2011-11-24 
2662 24/11/2011 19:55 1.3335 2011-11-24 
2663 24/11/2011 20:00 1.3334 2011-11-24 
2664 24/11/2011 20:05 1.3334 2011-11-24 
2665 24/11/2011 20:10 1.3339 2011-11-24 
2666 24/11/2011 20:15 1.3339 2011-11-24 
2667 24/11/2011 20:20 1.3346 2011-11-24 
2668 24/11/2011 20:25 1.3349 2011-11-24 
2669 24/11/2011 20:30 1.3342 2011-11-24 
2670 24/11/2011 20:35 1.3345 2011-11-24 
2671 24/11/2011 20:40 1.3347 2011-11-24 
2672 24/11/2011 20:45 1.3349 2011-11-24 
2673 24/11/2011 20:50 1.3349 2011-11-24 
2674 24/11/2011 20:55 1.3349 2011-11-24 
2675 24/11/2011 21:00 1.3349 2011-11-24 
2676 24/11/2011 21:05 1.3348 2011-11-24 
2677 24/11/2011 21:10 1.3349 2011-11-24 
2678 24/11/2011 21:15 1.3348 2011-11-24 
2679 24/11/2011 21:20 1.3348 2011-11-24 
2680 24/11/2011 21:25 1.3348 2011-11-24 
2681 24/11/2011 21:30 1.3348 2011-11-24 
2682 24/11/2011 21:35 1.3348 2011-11-24 
2683 24/11/2011 21:40 1.3346 2011-11-24 
2684 24/11/2011 21:45 1.3346 2011-11-24 
2685 24/11/2011 21:50 1.3347 2011-11-24 
2686 24/11/2011 21:55 1.3348 2011-11-24 
2687 24/11/2011 22:00 1.3354 2011-11-24 
2688 24/11/2011 22:05 1.3353 2011-11-24 
2689 24/11/2011 22:10 1.3349 2011-11-24 
2690 24/11/2011 22:15 1.3352 2011-11-24 
2691 24/11/2011 22:20 1.3348 2011-11-24 
2692 24/11/2011 22:25 1.3348 2011-11-24 
2693 24/11/2011 22:30 1.3349 2011-11-24 
2694 24/11/2011 22:35 1.3350 2011-11-24 
2695 24/11/2011 22:40 1.3348 2011-11-24 
2696 24/11/2011 22:45 1.3348 2011-11-24 
2697 24/11/2011 22:50 1.3347 2011-11-24 
2698 24/11/2011 22:55 1.3347 2011-11-24 
2699 24/11/2011 23:00 1.3345 2011-11-24 
2700 24/11/2011 23:05 1.3345 2011-11-24 
2701 24/11/2011 23:10 1.3345 2011-11-24 
2702 24/11/2011 23:15 1.3341 2011-11-24 
2703 24/11/2011 23:20 1.3339 2011-11-24 
2704 24/11/2011 23:25 1.3337 2011-11-24 
2705 24/11/2011 23:30 1.3338 2011-11-24 
2706 24/11/2011 23:35 1.3337 2011-11-24 
2707 24/11/2011 23:40 1.3341 2011-11-24 
2708 24/11/2011 23:45 1.3338 2011-11-24 
2709 24/11/2011 23:50 1.3335 2011-11-24 
2710 24/11/2011 23:55 1.3331 2011-11-24 

恆溫5個分鐘觀察到位已經。我基本上只是想知道如何修改這些數據以獲得每10分鐘,15分鐘,30分鐘和1小時的觀測值,即在讀取數據時順序刪除行,而不必手動刪除每個不需要的行通過MSExcel的時間,然後讀取數據?

+1

你的意思是像'逸〔序列(1,nrow(DAT),通過= ###),]'? – A5C1D2H2I1M1N2O1R2T1

回答

3
res <- 2 
## 2 for one every 10min (i. e. one every 2 rows), 
## 3 for one every 15min (i. e. one every 3 rows), etc. 
dat[seq(1, nrow(dat), by=res), ] 

編輯評論如下:

res <- 2 
split(dat, dat$day, drop=TRUE) -> d1 
##Split into days 
lapply(d1, FUN=function(x) {x[seq(1, nrow(x), by=res), ]->x}) -> d1 
##Select one observation every 10min for each day separately 
as.data.frame(do.call(rbind, d1)) -> result 
##Re-aggregate into one single dataframe (you may need to reset the row names though) 
+0

正是我在想什麼。你更快.... – A5C1D2H2I1M1N2O1R2T1

+0

嗨,感謝,這大部分工作的魅力,但不幸的是我的一些數據不是'完美的',因爲當我添加上面的代碼時,它會在例如以22:10,因此在第二天跳過00:00,從而每隔兩天就進行一次「錯誤」觀察。你知道有什麼方法可以阻止它嗎?謝謝 – number8

+0

簡單的答案就是在第一步中將數據集分爲幾天,然後每隔10分鐘選取一個觀測值,然後再將所有天數重新彙總。可能有辦法避免這種麻煩,但我現在看不到它...... – plannapus