Home > Software design >  How to fill previous value in a downwards direction in a dataframe till last numeric value of a colu
How to fill previous value in a downwards direction in a dataframe till last numeric value of a colu

Time:10-05

I have a data frame

df= data.frame(d= c("90-1","90-2","90-3","90-4","90-5","91-5","91-6"),
              a=c(NA,NA,150,NA,1,NA,3),
              b=c(1.0,NA,90.7,30.5,NA,NA,NA),
              c=c(NA,NA,1000,2,4,NA,NA))

Here the 1st column is a non-null column.
I want to fill the previous value if NA till the last numeric value of each column i.e from 2nd column to ncol(df).

Required Output

df_result= data.frame(d= c("90-1","90-2","90-3","90-4","90-5","91-5","91-6"),
               a=c(NA,NA,150,150,1,1,3),
               b=c(1.0,1.0,90.7,30.5,NA,NA,NA),
               c=c(NA,NA,1000,2,4,NA,NA)) 

Code Tried
rev(which(df$b>0))[1] would give me the last non numeric value of column b.
And fill function from tidyr would work But I don't know how to combine the two.

Any approach would be helpful.

CodePudding user response:

Here's one trick to do this.

For each column from a:c we replace the NA values after the last numeric value to string "NA", use fill to replace real NA values and change the "NA" value back to real NA's.

library(dplyr)
library(tidyr)

df %>%
  mutate(across(a:c, ~replace(., is.na(.) & 
                row_number() > max(which(!is.na(.))), "NA"))) %>%
  fill(a:c) %>%
  mutate(across(a:c, ~replace(., . == 'NA', NA))) %>%
  type.convert(as.is = TRUE)

#     d   a    b    c
#1 90-1  NA  1.0   NA
#2 90-2  NA  1.0   NA
#3 90-3 150 90.7 1000
#4 90-4 150 30.5    2
#5 90-5   1   NA    4
#6 91-5   1   NA   NA
#7 91-6   3   NA   NA
  • Related