2013-05-16 53 views
1

我的原始數據框顯示了幾個人(由Ring標識)在大約2周時間內對一個變量(動作,以秒爲單位)的更改。我的問題是這個變量延伸到日期的變化上(即在午夜),我想將它分成兩部分:從時間[i]直到午夜,從午夜直到時間[i + 1]。我已經增加了一些變數,我需要計算這兩個操作:如何在數據框中的可變位置插入行

  1. 修改第i行(日期更改只有在),所以[I]午夜
  2. 插入一個之前,我可以得到行動的一部分額外的行(僅當日期發生變化時)並將其分配給act [i]的另一部分。

例如:

ith row: 01-01-2000 23:55:00 act= 360 seconds 

i+1th row: 02-01-2000 00:01:00 act= 30 seconds 

i+2th row: 02-01-2000 00:01:30 act= 50 seconds 

. 
. 
. 

我的目標是獲得:

ith row: 01-01-2000 23:55:00 act= 300 seconds # modified row 

i+1th row: 02-01-2000 00:00:00 act= 60 seconds # inserted row 

i+2th row: 02-01-2000 00:01:00 act= 30 seconds # previously row i+1th 

i+3th row: 02-01-2000 00:01:30 act= 30 seconds #previously row i+2th 

. 
. 
. 

在不同的時間段相關聯的每個單獨的(環)拉伸,數據從而導致日期改變在不被考慮的個人之間。 下面,選擇我〜90000行數據幀(XACT)的顯示範圍內與個人之間(環)日期的變化和未來我的代碼:

 Ring    time act wd  date clock    timepos  timemn actmn jul 
156 6106933 09/06/11 21:37:45 267 dry 09/06/11 21:37:45 2011-06-09 21:37:45 2011-06-10 8535 15134 
157 6106933 09/06/11 21:42:12 3417 wet 09/06/11 21:42:12 2011-06-09 21:42:12 2011-06-10 8268 15134 
158 6106933 09/06/11 22:39:09 51 dry 09/06/11 22:39:09 2011-06-09 22:39:09 2011-06-10 4851 15134 
159 6106933 09/06/11 22:40:00 7317 wet 09/06/11 22:40:00 2011-06-09 22:40:00 2011-06-10 4800 15134 
160 6106933 10/06/11 00:41:57 24 dry 10/06/11 00:41:57 2011-06-10 00:41:57 2011-06-11 83883 15135 
529 6106933 11/06/11 22:41:57 3177 wet 11/06/11 22:41:57 2011-06-11 22:41:57 2011-06-12 4683 15136 
530 6106933 11/06/11 23:34:54  6 dry 11/06/11 23:34:54 2011-06-11 23:34:54 2011-06-12 1506 15136 
531 6106933 11/06/11 23:35:00 1779 wet 11/06/11 23:35:00 2011-06-11 23:35:00 2011-06-12 1500 15136 
532 6106933 12/06/11 00:04:39 594 dry 12/06/11 00:04:39 2011-06-12 00:04:39 2011-06-13 86121 15137 
533 6106933 12/06/11 00:14:33 18840 wet 12/06/11 00:14:33 2011-06-12 00:14:33 2011-06-13 85527 15137 
7024 6134701 24/07/11 15:24:14 6 dry 24/07/11 15:24:14 2011-07-24 15:24:14 2011-07-25 30946 15179 
7025 6134701 24/07/11 15:24:20 6 wet 24/07/11 15:24:20 2011-07-24 15:24:20 2011-07-25 30940 15179 
7026 6134701 24/07/11 15:24:26 810 dry 24/07/11 15:24:26 2011-07-24 15:24:26 2011-07-25 30934 15179 

R = unique(xact$Ring) 
for (m in R) { 
for (i in 1:nrow(xact)) { 
if(xact$jul[i] < xact$jul[i+1]) { 
    # modify row i (jul= Julian date) 
xact[i] <- c(xact$Ring[i], xact$time[i], xact$actmn[i], xact$wd[i], xact$date[i], xact$clock[i], xact$timepos[i], xact$timemn[i], xact$actmn[i], xact$jul[i]) 
    # add new row between row i and row i+1 
r <- i 
newrow <- c(xact$Ring[i], xact$timemn[i], as.numeric(xact$timepos[i+1] - xact$timemn[i]), xact$wd[i], xact$date[i+1], xact$clock[i+1], xact$timemn[i], xact$timemn[i], xact$actmn[i], xact$jul[i+1]) 
insertRow <- function(xact, newrow, r) { 
xact[seq(r+1, nrow(xact) + 1), ] <- xact[seq(r, nrow(xact)), ] 
xact[r,] <- newrow 
xact 
} 
} 
} 
} 

我試着去適應現有的代碼​​但產生此消息:

我將不勝感激任何幫助。

桑蒂

+2

你並不需要插入行。附加它們並對數據進行排序。你也可能不需要循環。如果您以某種方式提供數據,我們可以很容易地閱讀(使用'dput'),我們可以提出建議。 – Roland

+0

@Roland你應該把它作爲(最好的:-))回答。只需創建一個矩陣(runif ....)例子,以便OP瞭解如何對他的數據框進行「排序」或「排序」。順便說一句,笨重的方法是'df < - rbind(df [1:j] ,newrowdata,df [j + 1:nrow(df)])' –

+0

我已經出去了幾天了,對不起,沒有輸入,Carl是正確的,這裏是我的數據框的一部分壓縮: http ://www.megafileupload.com/en/file/419337/DF1001-zip.html –

回答

1

這裏是一個虛構的數據爲例:

#create data 
DF <- data.frame(time=seq(from=strptime("2013-01-01 01:00","%Y-%m-%d %H:%M"),to=strptime("2013-01-03 01:00","%Y-%m-%d %H:%M"),by=3500)) 
DF$ring <- 1:2 
DF <- DF[order(DF$ID),] 

#apply per ring 
library(plyr) 
DF <- ddply(DF,.(ring),function(df){ 
    #index of date changes 
    ind <- c(FALSE,diff(as.POSIXlt(df$time)$yday)==1) 
    add <- df[ind,] 
    add$time <- round(add$time,"days") 
    #you can simply rbind and order, no need for inserting 
    df <- rbind(df,add) 
    df <- df[order(df$time),] 
    #it's easier to calculate act here 
    df$act <- c(diff(as.numeric(df$time)),NA) 
    df}) 

        time ring act 
1 2013-01-01 01:00:00 1 7000 
2 2013-01-01 02:56:40 1 7000 
3 2013-01-01 04:53:20 1 7000 
4 2013-01-01 06:50:00 1 7000 
5 2013-01-01 08:46:40 1 7000 
6 2013-01-01 10:43:20 1 7000 
7 2013-01-01 12:40:00 1 7000 
8 2013-01-01 14:36:40 1 7000 
9 2013-01-01 16:33:20 1 7000 
10 2013-01-01 18:30:00 1 7000 
11 2013-01-01 20:26:40 1 7000 
12 2013-01-01 22:23:20 1 5800 
13 2013-01-02 00:00:00 1 1200 
14 2013-01-02 00:20:00 1 7000 
15 2013-01-02 02:16:40 1 7000 
16 2013-01-02 04:13:20 1 7000 
17 2013-01-02 06:10:00 1 7000 
18 2013-01-02 08:06:40 1 7000 
19 2013-01-02 10:03:20 1 7000 
20 2013-01-02 12:00:00 1 7000 
21 2013-01-02 13:56:40 1 7000 
22 2013-01-02 15:53:20 1 7000 
23 2013-01-02 17:50:00 1 7000 
24 2013-01-02 19:46:40 1 7000 
25 2013-01-02 21:43:20 1 7000 
26 2013-01-02 23:40:00 1 NA 
27 2013-01-01 01:58:20 2 7000 
28 2013-01-01 03:55:00 2 7000 
29 2013-01-01 05:51:40 2 7000 
30 2013-01-01 07:48:20 2 7000 
31 2013-01-01 09:45:00 2 7000 
32 2013-01-01 11:41:40 2 7000 
33 2013-01-01 13:38:20 2 7000 
34 2013-01-01 15:35:00 2 7000 
35 2013-01-01 17:31:40 2 7000 
36 2013-01-01 19:28:20 2 7000 
37 2013-01-01 21:25:00 2 7000 
38 2013-01-01 23:21:40 2 2300 
39 2013-01-02 00:00:00 2 4700 
40 2013-01-02 01:18:20 2 7000 
41 2013-01-02 03:15:00 2 7000 
42 2013-01-02 05:11:40 2 7000 
43 2013-01-02 07:08:20 2 7000 
44 2013-01-02 09:05:00 2 7000 
45 2013-01-02 11:01:40 2 7000 
46 2013-01-02 12:58:20 2 7000 
47 2013-01-02 14:55:00 2 7000 
48 2013-01-02 16:51:40 2 7000 
49 2013-01-02 18:48:20 2 7000 
50 2013-01-02 20:45:00 2 7000 
51 2013-01-02 22:41:40 2 4700 
52 2013-01-03 00:00:00 2 2300 
53 2013-01-03 00:38:20 2 NA 
相關問題