If cells in a column are empty, fill with cells from another column

Hey there,

I want to overlap columns in such a way that empty cells in column A are filled by entries from column B in the corresponding cell. Thanks for your help!

Best regards, Paula


overlap <- tibble(
  col_A = c(1, 2, 3, NA, NA),
  col_B = c(10, 20, 30, 40, 50)

overlap %>% 
  mutate(col_A = ifelse(is.na(col_A), col_B, col_A))
#> # A tibble: 5 × 2
#>   col_A col_B
#>   <dbl> <dbl>
#> 1     1    10
#> 2     2    20
#> 3     3    30
#> 4    40    40
#> 5    50    50

Thanks for your help! I've used your Code but there is still an error "error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, :
arguments imply differing number of rows: 46, 986184"

Could you provide a small reprex/example of your code and the data you are using?

What you asked, implies that both columns are in the same data.frame, but the error message you shared (to my mind at least) challenges that assumption). Can you confirm if your two source columns are in the same data.frame, or are you wanting to merge data.frames as an earlier step ?

Is there a function like fill() that will fill horizontally rather than vertically?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.