'R data.table melt converts NAs into (close to zero) values
I create an excel spreadsheet with some blanks (I never enter anything into those cells) in the second, third, etc. columns. The first column is the ID variable for the observation and is never missing. Furthermore, in the second through last columns, there all values are numeric and large positive numbers, certainly nothing close to zero. I save as .csv. I import to an R data.table using fread.
The blank cells are reflected as NA in R, and return TRUE when queried by is.na(). However, when displaying the cells in the RStudio viewer, some of the NAs are on the left side of the cell and some are on the right side of the cell. Further, when I display those values in the console, it returns <NA>, while the others (rendered on the left) return NA.
My data.table is called, say, data. I melt data to convert to long form, to collapse all of the columns beside the first one into two columns, i.e., I do
data <- melt(data, id.vars = 1, measure.vars = 2:length(names(data)))
When I then inspect the result, there are many entries in the value column equal to a numeric value very close to zero. It turns out that these correspond exactly to those in the original data.table with the NA value rendered on the right of the cell, as opposed to the left. I emphasize at this point that those very cells returned TRUE when queried by is.na() in the initial data.
This seems to have something to do with the difference between <NA> and NA, but I still don't see why under any circumstance melt should turn any NA or <NA> value into a non-missing value.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
