1

Let's say that I want to delete the data in ith row and jth col in a data table by setting it to NULL:

dt[i, j := NULL,with=F]

set(dt1,i,j,value=NULL)

Both of the above options gave me the error: When deleting columns, i should not be provided

However if I don't provide i , the entire column in the data table gets deleted, which is not what I want.

So how do I delete a single value in a datatable?

xyy
  • 547
  • 1
  • 5
  • 12
  • 5
    A single value can be `NA`, not `NULL`. Missing values in R are indicated with `NA` and they play the same role as NULL in some DBMS, like MySQL. – nicola May 17 '15 at 20:46
  • 1
    Deletion by reference is not yet supported. Possible dupe: http://stackoverflow.com/questions/10790204/how-to-delete-a-row-by-reference-in-r-data-table – Frank May 17 '15 at 22:21
  • 1
    I don't know how your data.table looks, but this works: `xy <- data.table(B = 1:10, C = 11:20); xy[4, 2] <- NA_integer_`. Will this get you what you want? – iembry May 18 '15 at 01:31
  • 1
    thank you everyone, I see that I can only set it to NA instead of NULL, this answers my question :) – xyy May 18 '15 at 16:31

1 Answers1

2

In the sense of a data.table you can't delete item in a cell. You can change its value to missing which (most likely) will achieve what you're looking for using something like

dt[i, j := NA]

This will set value of column j in row i to NA, or unset it, in other terms. Later when you want to work with these values or exclude them you could use is.na(..) similarly how you would think of is.null(..)

Regarding the error you're getting, j := NULL is used in data.table to delete an entire column, and indeed when you try to delete a part of it (by specifying i) it produced an error. If you're looking to remove an entire row from data table, you could use something like

dt <- dt[-i] # exclude row i from the data.table
Sergii Zaskaleta
  • 502
  • 1
  • 4
  • 21