Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert a row in a data.table

Tags:

r

data.table

If I have a data frame

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)

If I now do the equivalent in data.table

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

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

like image 211
Tahnoon Pasha Avatar asked May 20 '13 15:05

Tahnoon Pasha


People also ask

How do I add a row to a DataTable in Excel?

To insert a row, pick a cell or row that's not the header row, and right-click. To insert a column, pick any cell in the table and right-click. Point to Insert, and pick Table Rows Above to insert a new row, or Table Columns to the Left to insert a new column.

How do I add a row to a DataTable in R?

With command rbindlist from the data. table package, we can append dt_add_row and new_row row-wise. Object dt_add_row, shown in Table 2, shows the original data. table with the added row number 6.


1 Answers

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

As we can see:

                             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, ])) 

or even

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

giving:

            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 

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 

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

like image 143
dardisco Avatar answered Oct 08 '22 21:10

dardisco