R fill but only one way

enter image description here

The above is a table I created from a diagonal matrix style data.

the AYDM column is basically a cumulative of the months across the different years.

Now, it is in my interest to fill the blanks within the table to it’s most previous value( probably 0).

But NOT any values after the final value within each column – I would like those to remain blank.

I have tried using the fill function as such:

 Y_table2 <- pivot_wider(Y_table, names_from = !! rlang::sym(Reactive_year()), values_from = !! rlang::sym(Reactive_stats()))# %>%
      # fill(everything())

This didn’t quite work as to my understanding, fill function loops back to the start, whereas I want it to stop at the final value within each column.

Any ideas? Thank you in advance

  • 2

    Please do not post (only) an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: meta.stackoverflow.com/a/285557 and xkcd.com/2116). Please include the code, console output, or data (e.g., data.frame(...) or the output from dput(head(x))) directly into a code block.

    – 

  • 2

    I don’t understand your explanation. You say “NOT any values after the final value within each column”, which means the blanks (empty string "", since it appears your values are not numbers due to the lack of NAs) should remain blanks. That suggests what you want is to fill the top empty/NA values, which are not present.

    – 

Leave a Comment