0

I have a dataset with 7 columns and I am trying to make an 8th column based on conditions of 2 other columns. I've managed to calculate the first condition (take the last date before an NA), but I am not trying to fill in the row that does have an NA with the value from another column. I've tried following this thread but having trouble: R - Fill Column with values from any other columns

Here is a subset of my example data with the relevant columns:


df <- data.frame( id = c(1,1,1,2,2,2,3,3,3),
                  date_1 = c(2020-03-04, 2021-05-14, NA, 2020-03-16,2021-07-04,2022-03-04,2020-03-04, NA,NA),
                  date_2 = c(2020-07-07, 2020-07-07,2020-07-07, NA, NA, NA, 2020-11-04, 2020-11-04, 2020-11-04))

The code I am trying to use is:

last_date <-
group_by(id) %>%
  mutate(last_lact_date = dplyr::last(na.omit(date_2))) %>%
  mutate(last_lact_date = ifelse(is.na(date_2), NA, paste0(last_lact_date)))

The problem is that when I run this code, I still get NAs in the last_lact_date column where the date_1 is NA. I'd really like to have them filled in with the date 1.

Any thoughts?

Cae.rich
  • 171
  • 7
  • What is your expected output? Also, check the dataframe you provide, it is probably not what you intended to send (dates should be quoted). – Maël Sep 30 '22 at 10:05

0 Answers0