Replace NA with NULL

Viewed 6662

I have a sample vector with NA, I want to replace these NA with NULL

ts<-c(12,NA,45,16,48,69,NA,3)

I tried this but it didn't work

ts[is.na(ts)]<-NULL

is there anyway to do this? Thanks.

2 Answers

As far as i know a vector can't contain the value NULL. It will just get omitted. For example

ts <- c(1,2,NULL,NULL)
ts
[1] 1 2

Why do you want to do this replacement in the first place?

Doesn't make it a real NULL but does the job.

forcats::fct_explicit_na(as.character(ts),"NULL")
Related