rainbowthug
rainbowthug

Reputation: 77

How to replace NA's with blank value?

I am pretty new to R and I wonder if I can replace NA value (which looks like string) with blank, nothing

It is easy when entire table is as.character however my table contains double's as well therefore when I try to run

f <- as.data.frame(replace(df, is.na(df), ""))

or

df[is.na(df)] <- ""

Both does not work.

Error is like

Assigned data `values` must be compatible with existing data.
Error occurred for column `ID`.
Can't convert <character> to <double>.

and I understand, but I really need ID's as well as any other cell in the table (character, double or other) blank to remain in the table, later on it is connected to BI tool and I can't present "NA", just blank for the sake of clarity

Upvotes: 2

Views: 5658

Answers (1)

deschen
deschen

Reputation: 10996

If your column is of type double (numbers), you can't replace NAs (which is the R internal for missings) by a character string. And "" IS a character string even though you think it's empty, but it is not.

So you need to choose: converting you whole column to type character or leave the missings as NA.

EDIT:

  • If you really want to covnert your numeric column to character, you can just use as.character(MYCOLUMN). But I think what you really want is:
  • Telling your exporting function how to treat NA'S, which is easy, e.g. write.csv(df, na = ""). Also check the help function with ?write.csv.

Upvotes: 4

Related Questions