1

I have a data.table from which I want to remove those rows which have NA value in any column.

a <- data.table(s = c(1, 2, 3, NA, NA, 3, 2), d = c(32, 23, 2, 121, NA, 3, NA))

So to remove NA values from this data.table, I will have to assign it back to a:-

a <- na.omit(a)

In this way it will create a new object a in a new memory address.

I want to know if there is a way in which I can use data.table syntax ([i, j, by]) which can remove the NA values, take care of the assignment and not create a new memory address for a.

Shawn Brar
  • 1,346
  • 3
  • 17
  • 3
    No, I don't think that's possible. You are changing the size of the vectors in the data.table. I don't think a copy can be avoided. – Roland Jun 04 '20 at 08:40
  • 4
    Related: [How to delete a row by reference in data.table?](https://stackoverflow.com/questions/10790204/how-to-delete-a-row-by-reference-in-data-table); "data.table can't delete rows by reference yet" – Henrik Jun 04 '20 at 08:46
  • 1
    Which packages is data.table from? – JAQuent Jun 04 '20 at 08:59
  • 2
    Perhaps you can avoid these rows earlier, depending on how you are reading in the data. @JAQuent `data.table()` is, .... (surprise), from the `data.table` package. – s_baldur Jun 04 '20 at 09:02
  • 1
    I'm taking that comment/question as a reminder to myself. Several times, I've asked about functions/packages implied by a question, only to realize that the tags indicated the package(s). It's easy (for me, at least) to go through a question and not even *register* the tags (heck, I've even skipped a paragraph a few times). – r2evans Jun 04 '20 at 20:13

0 Answers0