How to connect dots where there are missing values?

后端 未结 4 1680
天涯浪人
天涯浪人 2021-01-04 19:01

lets take this example:

       test=c(1,5,NA,5,4,1,NA,3,3,5,4,2)

      plot(test,type=\"l\")

This will plot test but will not connect the

4条回答
  •  暖寄归人
    2021-01-04 19:41

    Either you have to ignore the NAs with the mentioned solutions using na.omit() or you try to replace the NAs with reasonable values - you can use the package imputeTS for this.

    You could e.g. interpolate:

    library(imputeTS)
    imp <- na.interpolation(test)
    plot(imp, type="l")
    

    You could take the mean as replacement:

    library(imputeTS)
    imp <- na.mean(test)
    plot(imp, type="l")
    

    You could also take the moving average as replacment:

    library(imputeTS)
    imp <- na.ma(test)
    plot(imp, type="l")
    

    In the end, it makes sense to use what is best for your use case. Oftentimes this will be 'ignoring' the NAs - since interpolation/imputation is only a estimation of the real values and also requires selecting the right method.

提交回复
热议问题