0

I have this tibble (transactions) A tibble: 13 × 2

  START_DATE CHANNEL_LVL1
   <chr>      <chr>       
 1 NA         Website     
 2 2023-01-31 Website     
 3 NA         WalkIn      
 4 2023-01-31 Marketplace 
 5 NA         Marketplace 
 6 2023-01-31 Marketplace 
 7 2023-01-31 Call Center 
 8 2023-01-31 Marketplace 
 9 2023-01-31 Marketplace 
10 NA         WalkIn      
11 2023-01-31 Website     
12 2023-01-31 Website     
13 2023-01-31 Marketplace 

I want to check for every row if( (is.na(START_DATE) && CHANNEL_LVL1 =="WalkIn"). If that's true, I want to replace NA with date_to_set.

I tried this approach

transactions <- transactions %>% 
mutate( 
START_DATE = ifelse(is.na(START_DATE) & CHANNEL_LVL1 == "WalkIn", date_to_set, as.character(START_DATE)
) 

It produces this error :

Error in mutate(): ! Problem while computing START_DATE = ifelse(CHANNEL_LVL1 == "WalkIn" & is.na(START_DATE), date, START_DATE). Caused by error: ! START_DATE must return compatible vectors across groups. Run rlang::last_error() to see where the error occurred. Called from: signal_abort(cnd, .file)

I have also tried case_when approach to no avail.

Any help will be appreciated!

1 Answers1

0

The following works:

transactions <- read.table(text="
 START_DATE CHANNEL_LVL1
 1 NA         Website     
 2 2023-01-31 Website     
 3 NA         WalkIn      
 4 2023-01-31 Marketplace 
 5 NA         Marketplace 
 6 2023-01-31 Marketplace 
 7 2023-01-31 'Call Center' 
 8 2023-01-31 Marketplace 
 9 2023-01-31 Marketplace 
10 NA         WalkIn      
11 2023-01-31 Website     
12 2023-01-31 Website     
13 2023-01-31 Marketplace", header=T)

date_to_set <- "2023-01-31"

transactions %>% 
  mutate(START_DATE = ifelse(is.na(START_DATE) & CHANNEL_LVL1 == "WalkIn", date_to_set, as.character(START_DATE))) 

My guess from your error ('START_DATE must return compatible vectors across groups') is that your dataframe is grouped without you realising it, or maybe date_to_set is something unusual (you should include it), or perhaps that START_DATE is a character column, and date_to_set is a date or date column, and the two aren't mixing well.

Mark
  • 7,785
  • 2
  • 14
  • 34