r - 根据列条目插入行,并根据插入的位置确定插入行的条目

标签 r insert row

我有这些数据,我将其命名为 A:

A <- read.table(text = "ID  TIME    EVID    AMT DOSE
1   10      1       100 20
1   12      1       100 20
1   14      1       100 20
1   16      1       100 20
1   17      0       100 20
1   18      1       100 20
1   20      1       100 20
1   22      1       100 20
2   5       1       100 40
2   10      1       100 40
2   15      1       100 40
2   17      0       100 40
2   20      1       100 40
3   4       1       100 25
3   7       1       100 25
3   10      1       100 25
3   11      0       100 25
3   13      1       100 25
3   16      1       100 25
3   19      1       100 25", header = TRUE)

我的目标是插入 EVID=2 的新行,ID 与前一行 ID 相同,TIME = 前一行的 TIME 条目加上 AMT/DOSE,我希望新行在第一个 EVID=1 之后跟随0s,如下:
ID  TIME    EVID    AMT DOSE
1   10      1       100 20
1   12      1       100 20
1   14      1       100 20
1   16      1       100 20
1   17      0       100 20
1   18      1       100 20
1   23      2       100 20
1   20      1       100 20
1   22      1       100 20
2   5       1       100 40
2   10      1       100 40
2   15      1       100 40
2   17      0       100 40
2   20      1       100 40
2   22.5    2       100 40
3   4       1       100 25
3   7       1       100 25
3   10      1       100 25
3   11      0       100 25
3   13      1       100 25
3   17      2       100 25
3   16      1       100 25
3   19      1       100 25

我尽可能索引我的 EVID
rle(as.character(EVID))$lengths
A$Index<-unlist(sapply(rle(as.character(EVID))$lengths, seq_len), use.names = FALSE)

在这种情况下,此代码比 ave(EVID, EVID, FUN=seq_along) 更有效,它会索引所有 1 和所有 0,而不管它们是否连续。我想在 Index=1 和 Index=2 行之间插入我的新行(我将手动删除第一个新行)。
   ID TIME EVID AMT DOSE Index
1   1   10    1 100   20     1
2   1   12    1 100   20     2
3   1   14    1 100   20     3
4   1   16    1 100   20     4
5   1   17    0 100   20     1
6   1   18    1 100   20     1
7   1   20    1 100   20     2
8   1   22    1 100   20     3
9   2    5    1 100   40     4
10  2   10    1 100   40     5
11  2   15    1 100   40     6
12  2   17    0 100   40     1
13  2   20    1 100   40     1
14  3    4    1 100   25     2
15  3    7    1 100   25     3
16  3   10    1 100   25     4
17  3   11    0 100   25     1
18  3   13    1 100   25     1
19  3   16    1 100   25     2
20  3   19    1 100   25     3

结果 A 有一个新的 Index 列;我希望新行在索引 1 和 2 之间,即在本例中在行号 1、6、13 和 19 之后。

我遇到过 solutions我们可以在其中创建一个列向量,然后按照定义的行号将该列作为行插入到数据中。如何根据列条目添加行并动态确定一些条目?

谢谢你的帮助!

最佳答案

这是 data.table 的解决方案
真的只是两行代码(有一点注释)

library(data.table)
ADT <- data.table(row=1:nrow(A), A, key="ID")

# just to give an idea of how we can Find the first 0 after the first 1, look at the output from this
ADT[, list(row, EVID,c(NA,diff(EVID)), c(NA,diff(EVID))==1)]

# identify afer which row to insert
# the values you want to change, assign using the `=`
# the values to keep, just call the variable name, no `=` sign
newRows <- ADT[c(NA,diff(EVID))==1, list(row=row+1, ID, TIME=TIME+AMT/DOSE, EVID=2, AMT, DOSE)]

# rbind the new rows with the original DT
# then reverse order by EVID, and order by row.  
# After ordering, remove the first column (`row`) since it is not needed
newA <- rbind(ADT, newRows)[order(EVID, decreasing=TRUE)][order(row)][, -1, with=FALSE]


### Results: 

 > newA
    ID TIME EVID AMT DOSE
 1:  1   10    1 100   20
 2:  1   12    1 100   20
 3:  1   14    1 100   20
 4:  1   16    1 100   20
 5:  1   17    0 100   20
 6:  1   18    1 100   20
 7:  1   23    2 100   20
 8:  1   20    1 100   20
 9:  1   22    1 100   20
10:  2    5    1 100   40
11:  2   10    1 100   40
12:  2   15    1 100   40
13:  2   17    0 100   40
14:  2   20    1 100   40
15:  2   22    2 100   40
16:  3    4    1 100   25
17:  3    7    1 100   25
18:  3   10    1 100   25
19:  3   11    0 100   25
20:  3   13    1 100   25
21:  3   17    2 100   25
22:  3   16    1 100   25
23:  3   19    1 100   25
    ID TIME EVID AMT DOSE

关于r - 根据列条目插入行,并根据插入的位置确定插入行的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15264362/

相关文章:

R - 如何将这个嵌套的 for 循环转换为可以改变列表的 lapply 函数

r - 转置嵌套列表

r - dplyr 根据具有不同后缀的其他列进行变异

c# - 为什么 BLToolkit 在生成的查询中插入\0 而不是 '@' 符号?

perl - awk 镜像(反向)行

r - 如何将条形轮廓添加到 R 绘图条形图中?

c++ - 将数据插入数据结构时是深拷贝还是浅拷贝?

sql - 在 Postgres 中创建触发器

C# DataTable 在控制台中显示单行

html - Plotly Dash 不能将 div 放在同一行