Column1 Column2 Output
Match Match Match
Match NA Match
NA NA NA
以上是两列,Rä¸çš„所需输出输出应该是,如果两列都匹é…,它应该给出"匹é…",如果一列有匹é…而å¦ä¸€åˆ—有NA,它应该ä»æ‰“å°ï¼†ï¼ƒ34;匹é…"。但如果它们都是NA,则应将其打å°ä¸ºNA
ç”案 0 :(得分:2)
我们å¯ä»¥ä½¿ç”¨pmax
df1$Output <- do.call(pmax, c(df1, na.rm = TRUE))
df1$Output
#[1] "Match" "Match" NA
或coalesce
dplyr
library(dplyr)
df1 %>%
mutate(Output = coalesce(Column1, Column2))
# Column1 Column2 Output
#1 Match Match Match
#2 Match <NA> Match
#3 <NA> <NA> <NA>
df1 <- structure(list(Column1 = c("Match", "Match", NA), Column2 = c("Match",
NA, NA)), .Names = c("Column1", "Column2"), row.names = c(NA,
-3L), class = "data.frame")
ç”案 1 :(得分:1)
> df=NULL
> df$Column1=c("Match","Match",NA)
> df$Column2=c("Match",NA,NA)
> df=data.frame(df)
> df
Column1 Column2
1 Match Match
2 Match <NA>
3 <NA> <NA>
> df$Column3=ifelse(is.na(df$Column1)&is.na(df$Column2),NA,"Match")
> df
Column1 Column2 Column3
1 Match Match Match
2 Match <NA> Match
3 <NA> <NA> <NA>