Home > Back-end >  How do I replace nulls in one column with values in another column in the same dataframe in R
How do I replace nulls in one column with values in another column in the same dataframe in R

Time:03-08

My df in R has the following column contents (all char):

  • ColumnA - point-of-origin names
  • ColumnB - destination names
  • ColumnC - start coordinates
  • ColumnD - end coordinates
my_df <- structure(list(origin = c("AAA", "", "CCC", "DDD", "EEE"), destin = c("BBB", "FFF", "GGG", "HHH", "III"), start_coord = c("1.1", "2.2", "3.3", "4.4", "5.5"), end_coord = c("2.2", "6.6", "7.7", "8.8", "9.9" )), row.names = c(NA, 5L), class = "data.frame")

There are rows with empty strings in ColumnA. I want to replace the empty strings with values from ColumnB where ColumnC values match ColumnD values. The expected output for the missing value in the example above is BBB. How can I arrive at this? I'm new to R. Thanks.

CodePudding user response:

I'm sure there's a cleaner way to do this, but at first blush I'd be inclined to replace the blanks with a lookup using match(). If origin is non-blank, we just use origin otherwise we get the destin value for where the start_coord matches the end_coord:

df$origin <- ifelse(
  df$origin != "", 
  df$origin, 
  df$destin[match(df$start_coord, df$end_coord)]
)
  • Related