如何在数据框中的可变位置插入行

时间:2013-05-16 10:25:46

标签: r dataframe

我的原始数据框显示了一个变量(行为,以秒为单位)对几个人(由Ring确定)大约2周时间的变化。我的问题是这个变量延伸了日期的变化(即午夜),我想把它分成两部分:从时间[i]到午夜,从午夜到时间[i + 1]。我添加了一些计算这两个操作所需的变量:

  1. 修改第i行(仅在日期更改时),以便我可以在午夜之前获取act [i]的部分
  2. 插入一行(仅在日期更改时)并将其分配给act [i]。
  3. 的其他部分

    例如:

    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
    }
    }
    }
    }
    

    我尝试调整现有代码Add new row to dataframe, at specific row-index, not appended?,但会生成以下消息:

    我将不胜感激。

    桑蒂

1 个答案:

答案 0 :(得分: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