在 data.table 中插入一行 [英] Insert a row in a data.table

查看:19
本文介绍了在 data.table 中插入一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个数据框

set.seed(12345) 
df=data.frame(a=rnorm(5),b=rnorm(5))

我可以通过例如添加一行

I can add a row by e.g.

df[6,] =c(5,6)

如果我现在在 data.table 中做同样的事情

If I now do the equivalent in data.table

library(data.table)
dt=data.table(df)
dt[6,]=c(5,6)

它失败并出现错误.将行插入 data.table 的正确方法是什么?

It fails with an error. What is the right way to insert a row into a data.table?

推荐答案

为了扩展@Franks 的答案,如果在您的特定情况下您要附加一行,它是:

To expand on @Franks answer, if in your particular case you are appending a row, it's :

set.seed(12345) 
dt1 <- data.table(a=rnorm(5), b=rnorm(5))

以下是等价的;我发现第一个更容易阅读,但第二个更快:

The following are equivalent; I find the first easier to read but the second faster:

microbenchmark(
  rbind(dt1, list(5, 6)),
  rbindlist(list(dt1, list(5, 6)))        
  )

如我们所见:

                             expr     min      lq  median       uq     max
           rbind(dt1, list(5, 6)) 160.516 166.058 175.089 185.1470 457.735
 rbindlist(list(dt1, list(5, 6))) 130.137 134.037 140.605 149.6365 184.326

如果你想在别处插入该行,下面的方法会起作用,但它并不漂亮:

If you want to insert the row elsewhere, the following will work, but it's not pretty:

rbindlist(list(dt1[1:3, ], list(5, 6), dt1[4:5, ]))

甚至

rbindlist(list(dt1[1:3, ], as.list(c(5, 6)), dt1[4:5, ]))

给予:

            a          b
1:  0.5855288 -1.8179560
2:  0.7094660  0.6300986
3: -0.1093033 -0.2761841
4:  5.0000000  6.0000000
5: -0.4534972 -0.2841597
6:  0.6058875 -0.9193220

如果您正在就地修改行(这是首选方法),则需要提前定义 data.table 的大小,即

If you are modifying a row in place (which is the preferred approach), you will need to define the size of the data.table in advance i.e.

dt1 <- data.table(a=rnorm(6), b=rnorm(6))
set(dt1, i=6L, j="a", value=5) # refer to column by name
set(dt1, i=6L, j=2L, value=6) # refer to column by number

感谢@Boxuan,我已经修改了这个答案以考虑你的建议,它更快更容易阅读.

Thanks @Boxuan, I have modified this answer to take account of your suggestion, which is a little faster and easier to read.

这篇关于在 data.table 中插入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆