如果列中存在 NA,则替换为另一列中的值 - sqldf
If NA exists in column, replace with value in another column - sqldf
我在下面有一个数据框:
df
ColA ColB ColC
NA BN 6
JH NA 8
NA rewr 9
NA NA 10
预期输出:
newdf
ColA ColB ColC New_Col
NA BN 6 BN
JH NA 8 JH
NA rewr 9 rewr
NA NA 10 NA
如何使用 sqldf 执行此操作?
这是我的尝试,但没有得到我正在寻找的输出:
newdf<- sqldf("SELECT *, replace([ColA], NULL, [ColB]) [New_Col] from df")
使用coalesce
library(sqldf)
sqldf("SELECT ColA, ColB, ColC, coalesce(ColA, ColB) as New_Col from df")
# ColA ColB ColC New_Col
#1 <NA> BN 6 BN
#2 JH <NA> 8 JH
#3 <NA> rewr 9 rewr
#4 <NA> <NA> 10 <NA>
或 tidyverse
library(dplyr)
df %>%
mutate(New_Col = coalesce(ColA, ColB))
# ColA ColB ColC New_Col
#1 <NA> BN 6 BN
#2 JH <NA> 8 JH
#3 <NA> rewr 9 rewr
#4 <NA> <NA> 10 <NA>
数据
df <- structure(list(ColA = c(NA, "JH", NA, NA), ColB = c("BN", NA,
"rewr", NA), ColC = c(6L, 8L, 9L, 10L)), class = "data.frame", row.names = c(NA,
-4L))
我在下面有一个数据框:
df
ColA ColB ColC
NA BN 6
JH NA 8
NA rewr 9
NA NA 10
预期输出:
newdf
ColA ColB ColC New_Col
NA BN 6 BN
JH NA 8 JH
NA rewr 9 rewr
NA NA 10 NA
如何使用 sqldf 执行此操作?
这是我的尝试,但没有得到我正在寻找的输出:
newdf<- sqldf("SELECT *, replace([ColA], NULL, [ColB]) [New_Col] from df")
使用coalesce
library(sqldf)
sqldf("SELECT ColA, ColB, ColC, coalesce(ColA, ColB) as New_Col from df")
# ColA ColB ColC New_Col
#1 <NA> BN 6 BN
#2 JH <NA> 8 JH
#3 <NA> rewr 9 rewr
#4 <NA> <NA> 10 <NA>
或 tidyverse
library(dplyr)
df %>%
mutate(New_Col = coalesce(ColA, ColB))
# ColA ColB ColC New_Col
#1 <NA> BN 6 BN
#2 JH <NA> 8 JH
#3 <NA> rewr 9 rewr
#4 <NA> <NA> 10 <NA>
数据
df <- structure(list(ColA = c(NA, "JH", NA, NA), ColB = c("BN", NA,
"rewr", NA), ColC = c(6L, 8L, 9L, 10L)), class = "data.frame", row.names = c(NA,
-4L))