Home > Enterprise >  How can I merge two columns in R?
How can I merge two columns in R?

Time:10-15

My dataframe has 2 columns which look similar to this:

a    b

NA   NA
yes  NA
no   NA
yes  NA
NA   yes
NA   no 
NA   NA
Na   yes

What I would then like as output is:

ab

NA
yes
no
yes
yes
no
NA
yes

Note that:

  • In the original columns, there will always be an NA in any given row.
  • For certain rows both columns will be NA

Any idea how I could get to the desired output?

CodePudding user response:

  • dplyr::coalesce
> with(dat, dplyr::coalesce(a, b))
[1] NA    "yes" "no"  "yes" "yes" "no"  NA    "yes"
  • ifelse for base R
> with(dat, ifelse(!is.na(a), a, ifelse(!is.na(b), b, NA)))
[1] NA    "yes" "no"  "yes" "yes" "no"  NA    "yes"
  • max.col for base R
> dat[cbind(1:nrow(dat), max.col(!is.na(dat)))]
[1] NA    "yes" "no"  "yes" "yes" "no"  NA    "yes"

CodePudding user response:

Using dplyr you can wrap it pretty neatly:

library(dplyr)

df %>% rowwise() %>% summarize(ab = max(a,b, na.rm = T))

CodePudding user response:

dat <- data.frame(a = c(NA, "yes", "no", "yes", NA, NA, NA, NA),
                  b = c(NA, NA, NA, NA, "yes", "no", NA, "yes"))

require(tidyverse)

dat %>% 
  rowwise() %>% 
  mutate(ab = max(a,b, na.rm = TRUE))

CodePudding user response:

Use apply:

> apply(df, 1, max, na.rm=TRUE)
[1] NA    "yes" "no"  "yes" "yes" "no"  NA    "yes"

Assignment:

df$ab <- apply(df, 1, max, na.rm=TRUE)

CodePudding user response:

# Import data: df => data.frame 
df <- structure(list(a = c(NA, "yes", "no", "yes", NA, NA, NA, NA), 
b = c(NA, NA, NA, NA, "yes", "no", NA, "yes")), class = "data.frame", row.names = c(NA, 
-8L))

# Function to coalesce vectors: br_coalesce => function
br_coalesce <- function(...){
   # Coalesce vectors or data.frames: res => vector
   res <- Reduce(function(x, y) {
         x <- replace(x, is.na(x), y[is.na(x)])
      },
      list(...)
   )
   # Explicitly define returned vectors: character vector => env
   return(res)
}

# Apply function: character vector / data.frame => stdout(console)
br_coalesce(df$a, df$b)

Tidyverse solution:

library(tidyverse)
df %>% 
   transmute(res = coalesce(a, b))

data.table solution:

library(data.table)
fcoalesce(df$a, df$b)
  • Related