我的数据框如下图所示。我要做的是在'2nd_Booking_Deadline'列为空(“”)的情况下,然后将其替换为'3rd_Booking_Deadline'中的值。一旦将“ 2nd_Booking_Deadline”替换为“ 3rd_Booking_Deadline”中的值,我需要将“ 3rd_Booking_Deadline”空白(“”)。
我已经编写了下面的代码,看起来可以完成该任务的第一部分,但返回的警告使我有些紧张:
Warning message:
In AAR_Combined_w_LL$`2nd_Booking_Deadline`[AAR_Combined_w_LL$`2nd_Booking_Deadline` == :
number of items to replace is not a multiple of replacement length
这是我到目前为止提出的:
AAR_Combined_w_LL$`2nd_Booking_Deadline`[AAR_Combined_w_LL $`2nd_Booking_Deadline` == ""] <- AAR_Combined_w_LL$`3rd_Booking_Deadline`
答案 0 :(得分:1)
在替换时,您需要从两端进行子集化。试试:
#Get the indices where `2nd_Booking_Deadline` is blank
inds <- AAR_Combined_w_LL $`2nd_Booking_Deadline` == ""
#Replace those blank values from the corresponding indices
AAR_Combined_w_LL$`2nd_Booking_Deadline`[inds] <- AAR_Combined_w_LL$`3rd_Booking_Deadline`[inds]
#Change `3rd_Booking_Deadline` to blank string
AAR_Combined_w_LL $`3rd_Booking_Deadline`[inds] = ""