Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does gsub not replace NA

Tags:

r

Aim

Replace NA with "Nothing" in character vector

Input

data<-c(NA, NA, "SupineAcid", NA, NA, NA, "UprightAcid", "UprightAcid", 
NA, NA, "UprightAcid", NA, "UprightAcid", NA, NA, "UprightAcid", 
"TotalAcid", NA, NA, NA)

Attempts

gsub(NA,"dd",data)

This leads to all the results being NA

I've also tried with "NA" and fixed=TRUE but the same issue.

like image 478
Sebastian Zeki Avatar asked Mar 05 '23 02:03

Sebastian Zeki


1 Answers

In order to change the NA elements in your vector, you can use the is.na function:

data[is.na(data)] = "dd"

 "dd"          "dd"          "SupineAcid"  "dd"          "dd"          "dd"          "UprightAcid"
 "UprightAcid" "dd"          "dd"          "UprightAcid" "dd"          "UprightAcid" "dd"         
 "dd"          "UprightAcid" "TotalAcid"   "dd"          "dd"          "dd"
like image 180
Lamia Avatar answered Mar 15 '23 15:03

Lamia