Replacing missing data in vector with existing data

前端 未结 2 746
猫巷女王i
猫巷女王i 2020-12-06 22:46

I have a vector (I actually just retrieved individual columns from a dataframe) which has missing data in it. I want to replace the missing data with the next available data

相关标签:
2条回答
  • 2020-12-06 23:08

    I want to add a next solution which using the runner r cran package.

    library(runner)
    > fill_run(data, run_for_first = T)
     [1] 5 5 5 5 5 5 5 7 7 7
    > rev(fill_run(rev(data), run_for_first = T))
     [1] 5 5 5 5 5 7 7 7 7 7
    

    The whole package is optimized and major of it was written in cpp. Thus offer a great efficiency.

    0 讨论(0)
  • 2020-12-06 23:25

    Maybe use na.locf...?

    library(zoo)
    na.locf(na.locf(zoo(data),fromLast = TRUE,na.rm = FALSE),na.rm = FALSE)
     1  2  3  4  5  6  7  8  9 10 
     5  5  5  5  5  7  7  7  7  7 
    
    0 讨论(0)
提交回复
热议问题