Windstorm1981
Windstorm1981

Reputation: 2680

Replace NAs in a Single Column of a Data Table in R

I'm trying to replace NAs in a single column of a datatable in R with "-999" and I can quite get it.

There is related question here on Stackoverflow but I think this can be done without iterating through the table.

I have a column, column_to_check in a datatable. The column is a factor variable and has 80K observations consisting of NA, 0, and 1. I'm trying to change the NA to -999 so I can do further work.

The code I'm working with is this:

is.na(DT[,column_to_check,with=FALSE]) = "-999"

and

DT[is.na(column_to_check), column_to_check:="-999"]

The first line sets the entire column to NA. The second doesn't work and I know is off but I think I'm close.

Can anyone help?

Thanks.

Upvotes: 1

Views: 4214

Answers (1)

Daniel Winkler
Daniel Winkler

Reputation: 497

Your code isn't off unless the data in the column is not a character in which case you would have to set -999 as inter/numeric without ""

data <- read.table(header=TRUE, text='
 id weight   size
 1     20  small
 2     27  large
 3     24 medium
 ')

data <- data.table(data)

> data[size == 'small', weight := NA]
> data
     size id weight
1:  small  1     NA
2:  large  2     27
3: medium  3     24
> is.na(data)
      size    id weight
[1,] FALSE FALSE   TRUE
[2,] FALSE FALSE  FALSE
[3,] FALSE FALSE  FALSE
> data[is.na(weight), weight := -999]
> data
     size id weight
1:  small  1   -999
2:  large  2     27
3: medium  3     24
> data[size == 'small', weight := NA]
> data[is.na(weight), weight := "-999"]
Warning message:
In `[.data.table`(data, is.na(weight), `:=`(weight, "-999")) :
  Coerced 'character' RHS to 'integer' to match the column's type. 

EDIT: This is, I just saw, what @dracodoc suggested in comment

Upvotes: 2

Related Questions