1

I have two columns

COL1   COL2
SCS    NA
NA     NA
NA     PB
NA     RM

Whenever col1 is na and col2 has a value, I want col2's value to overwrite the na. Whenever col1 has a value, I want it to stay that value no matter what is in col2.

  • Answers from this post will be helpful. https://stackoverflow.com/questions/27850344/coalesce-two-string-columns-with-alternating-missing-values-to-one – Ronak Shah Jul 03 '21 at 03:37

3 Answers3

3

This could be done with coalesce by specifying COL1 as first argument followed by COL2 so that if there is any NA in COL1 for a particular row, it will be replaced by the corresponding row from COL2

library(dplyr)
data <- data %>%
   mutate(COL1 = coalesce(COL1, COL2))

-output

data
COL1 COL2
1  SCS <NA>
2 <NA> <NA>
3   PB   PB
4   RM   RM

data

data <- structure(list(COL1 = c("SCS", NA, NA, NA), COL2 = c(NA, NA, 
"PB", "RM")), class = "data.frame", row.names = c(NA, -4L))
akrun
  • 874,273
  • 37
  • 540
  • 662
3

With the data.table package and assuming your data.frame is named df:

library(data.table)
setDT(df)

df[is.na(col1), col1 := col2]
DanY
  • 5,920
  • 1
  • 13
  • 33
1

This will replace values in col1 with values in col2 if a col1 value is `na

data <- data%>% 
  mutate(COL1 = case_when(is.na(COL1) ~ COL2,
                          TRUE ~ as.character(COL1)))
COL1   COL2
SCS    NA
NA     NA
PB     PB
RM     RM