dataframe - r simple way to merge 2 columns with not NA values -


this question has answer here:

how merge 1 column?

df <- data.frame(a = c("1", "2", na, na), b = c(na, na, "3", "4"), c = c(na, "5", "6", na), stringsasfactors = f)

i want values b without na. should be: "1", "2", "3", "4". (now need of choosing columns more 2 in df)

this works not good: df$a <- df$b[!is.na(df$b)]

we can use pmax

df$a <- do.call(pmax, c(df, na.rm = true)) 

or coalesce

library(dplyr) df %>%    mutate(a = coalesce(a,b)) #     b #1 1 <na> #2 2 <na> #3 3    3 #4 4    4 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -