0

I am merging two data frames however some data points are missing and I, therefore, want the merged data frame to use the last known value to overwrite the missing values.

Currently, my code is as follows:

CombData$`Price Close` <- CombData %>%
group_by(ISIN) %>%
mutate(na.locf(CombData$`Price Close`, na.rm = TRUE))

However, when looking at the output I can see that no missing values get replaced. Below is an example of how the data currently looks:

    ISIN            Date        Price Close
20  BSP951331318    2010-01-29  434.0
21  BSP951331318    2010-01-31  NA

Here I would like row 21 to have a closing price of 434.

peho15ae
  • 119
  • 9
  • 1
    Use `CombData %>% group_by(ISIN) %>% mutate(\`Price Close\` = na.locf(\`Price Close\`, na.rm = TRUE)) ` Or `CombData %>% group_by(ISIN) %>% fill(\`Price Close\`)` – Ronak Shah Feb 26 '20 at 12:42
  • When using the first code i get the following error message: Error: Column `Price Close` must be length 1037 (the group size) or one, not 960 – peho15ae Feb 26 '20 at 12:55
  • Use `na.locf0` like this: `CombData %>% group_by(ISIN) %>% mutate(\`Price Close\` = na.locf0(\`Price Close\`) ) %>% ungroup` – G. Grothendieck Feb 26 '20 at 13:57

1 Answers1

0

just na.locf(CombData) should be fine for this.

Jay
  • 1,181
  • 1
  • 9
  • 10