I am trying to replace NA with Blank using Sql in R. I used below code but I still get "NA" instead of blank!
DF_TEST_PN <- sqldf("Select *, Case when Proj_Loess ='NA' then ' ' else Proj_Loess end as New_Proj_Loess From Test")
You can use ifnull
in sqldf
/ sqLite
:
test <- tibble(Proj_Loess=c(NA,"test"))
DF_TEST_PN <- sqldf("Select *, ifnull(Proj_Loess,' ') as New_Proj_Loess From test")
Proj_Loess New_Proj_Loess
1 <NA>
2 test test
Suppose we have DF as shown below. The NAs in R are represented by null in SQL so use coalesce
as shown below. coalesce
looks at each argument in turn and returns the first one that is not null, ie not NA. coalesce
is available in all backends that sqldf supports (sqlite, h2, mysql, postgresql).
library(sqldf)
DF <- data.frame(A = c("A", NA, "B", NA, "C"))
sqldf("select *, coalesce(A, '') as V1 from DF")
giving:
A V1
1 A A
2 <NA>
3 B B
4 <NA>
5 C C
If you want to use case when
as in the question then use is null
like this:
sqldf("select *, case when A is null then '' else A end as V1 from DF")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.