Search code examples
rrstudiorowsblank-line

R Studio: How to replace the blank value with the corresponding row value of the second column


[R] Hi, for every row in my data i would to have the blanks in column REAL_TIME_ARR being replaced by the corresponding row value from column REAL_TIME_DEP. I cannot seem to get it to work for the hundreds of rows I have.

> TwentyFourSeptTrainData
REAL_TIME_ARR  REAL_TIME_DEP
               08:38
08:40          08:41
08:45          08:46
09:00          09:02
               09:07
09:10          09:11
and so on and so on

So my goal is for my data to look like this:

  > TwentyFourSeptTrainData
REAL_TIME_ARR  REAL_TIME_DEP
08:38          08:38
08:40          08:41
08:45          08:46
09:00          09:02
09:07          09:07
09:10          09:11

Thanks


Solution

  • TwentyFourSeptTrainData$REAL_TIME_ARR <- ifelse(TwentyFourSeptTrainData$REAL_TIME_ARR == "",
                                                    TwentyFourSeptTrainData$REAL_TIME_DEP,
                                                    TwentyFourSeptTrainData$REAL_TIME_ARR)
    

    Result:

    TwentyFourSeptTrainData
    REAL_TIME_ARR REAL_TIME_DEP
    1         08:38         08:38
    2         08:40         08:41
    3         08:45         08:46
    4         09:00         09:02
    5         09:07         09:07
    6         09:10         09:11