合并 df 中的列

Coalesce columns in df

structure(list(Primary.Warning.Vertical = c("N/A", "N/A", "N/A", 
"N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A"), Primary.Warning.Horizontal = c("2", 
"2", "1.1", "2", "2", "2", "1.7", "2", "2", "2"), Secondary.Sensor.Warning.Vertical = c("N/A", 
"N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A"
), Secondary.Sensor.Warning.Horizontal = c("N/A", "N/A", "N/A", 
"N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A")), row.names = c(NA, 
10L), class = "data.frame")

在较大的 df 中,这 4 列中的一列将包含一个值。我正在尝试创建一个将非 NA 值合并为一个的列

我试过:

df %>% mutate(TotalWarning = coalesce(Primary.Warning.Vertical, Primary.Warning.Horizontal, Secondary.Sensor.Warning.Vertical, Secondary.Sensor.Warning.Horizontal)) 

但 TotalWarning 列充满了 NA。

感谢您的帮助!

coalesce() 仅适用于“真实”缺失值。在你的数据中, "N/A" 是字符,所以首先你需要将它们转换为 NA.

library(dplyr)

df %>%
  mutate(across(where(is.character), na_if, "N/A"),
         TotalWarning = coalesce(Primary.Warning.Vertical,
                                 Primary.Warning.Horizontal,
                                 Secondary.Sensor.Warning.Vertical,
                                 Secondary.Sensor.Warning.Horizontal))

#    Primary.Warning.Vertical Primary.Warning.Horizontal Secondary.Sensor.Warning.Vertical Secondary.Sensor.Warning.Horizontal TotalWarning
# 1                      <NA>                          2                              <NA>                                <NA>            2
# 2                      <NA>                          2                              <NA>                                <NA>            2
# 3                      <NA>                        1.1                              <NA>                                <NA>          1.1
# 4                      <NA>                          2                              <NA>                                <NA>            2
# 5                      <NA>                          2                              <NA>                                <NA>            2
# 6                      <NA>                          2                              <NA>                                <NA>            2
# 7                      <NA>                        1.7                              <NA>                                <NA>          1.7
# 8                      <NA>                          2                              <NA>                                <NA>            2
# 9                      <NA>                          2                              <NA>                                <NA>            2
# 10                     <NA>                          2                              <NA>                                <NA>            2

你的变量名太繁琐了。为了简化代码,你也可以这样做:

df %>%
  mutate(across(where(is.character), na_if, "N/A"),
         TotalWarning = do.call(coalesce, cur_data()))