假設我有一個這樣的資料集:
a b
dog <NA>
mouse <NA>
chad cat
bird <NA>
我想合并這些列并像這樣解決沖突
a
dog
mouse
cat
bird
因此,a如果column 中存在值,它會覆寫 column中的值b。如果 column 中有 NA b,請忽略。
uj5u.com熱心網友回復:
有來自 dplyr 的合并功能
library(tidyverse)
df_example %>%
mutate(c = coalesce(b,a))
uj5u.com熱心網友回復:
另一種解決方案:
ifelse(is.na(df$b),df$a,df$b)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/333117.html
