使用na.locf函数在列末尾的NA

时间:2017-11-11 20:49:41

标签: r zoo

我使用zoo包和na.locf替换NA中的最后一个出现值。我的问题是如果最后一行没有数字,函数会失败。请看我的例子。

> tail(Dax1Dax2Dax3)
          Time    Res1    Accum1    Res2    Accum2    Res3    Accum3
2   2017-10-18 -109.41 171103.61    <NA>      <NA>    <NA>      <NA>
1   2017-10-20 1174.60 172278.21    <NA>      <NA>    <NA>      <NA>
406 2017-10-23    <NA>      <NA> 1473.25 185076.53    <NA>      <NA>
664 2017-10-25    <NA>      <NA>    <NA>      <NA>  -80.19 188974.10
663 2017-10-26    <NA>      <NA>    <NA>      <NA>  398.16 189372.27
662 2017-11-07    <NA>      <NA>    <NA>      <NA> -348.37 189023.90

require(zoo)

Dax1Dax2Dax3$Accum1 <- na.locf(Dax1Dax2Dax3$Accum1, fromLast = TRUE)
Error in `$<-.data.frame`(`*tmp*`, Accum1, value = c(253L, 253L, 253L,  : 
  replacement has 913 rows, data has 917

> dim(Dax1Dax2Dax3)
[1] 917   7

3 个答案:

答案 0 :(得分:3)

根据您的需要尝试其中一项。前两个留下了尾随的NA,第三个替代扩展了最后一个值。

# 1
Dax1Dax2Dax3$Accum1 <- na.locf(Dax1Dax2Dax3$Accum1, fromLast = TRUE, na.rm = FALSE)

# 2
Dax1Dax2Dax3$Accum1 <- na.locf0(Dax1Dax2Dax3$Accum1, fromLast = TRUE)

# 3
Dax1Dax2Dax3$Accum1 <- na.locf(Dax1Dax2Dax3$Accum1, fromLast = TRUE, rule = 1)

有关详细信息,请参阅?na.locf

注意:可重复形式的输入是:

Lines <- "
          Time    Res1    Accum1    Res2    Accum2    Res3    Accum3
2   2017-10-18 -109.41 171103.61    <NA>      <NA>    <NA>      <NA>
1   2017-10-20 1174.60 172278.21    <NA>      <NA>    <NA>      <NA>
406 2017-10-23    <NA>      <NA> 1473.25 185076.53    <NA>      <NA>
664 2017-10-25    <NA>      <NA>    <NA>      <NA>  -80.19 188974.10
663 2017-10-26    <NA>      <NA>    <NA>      <NA>  398.16 189372.27
662 2017-11-07    <NA>      <NA>    <NA>      <NA> -348.37 189023.90"

Dax1Dax2Dax3 <- read.table(text = Lines, na.strings = "<NA>")

答案 1 :(得分:0)

我们可以使用fill包中的tidyr函数将NA归为最后一个值。

library(tidyr)
Dax1Dax2Dax3 %>% fill(Res1, Accum1, Res2, Accum2)
#           Time    Res1   Accum1    Res2   Accum2    Res3   Accum3
# 2   2017-10-18 -109.41 171103.6      NA       NA      NA       NA
# 1   2017-10-20 1174.60 172278.2      NA       NA      NA       NA
# 406 2017-10-23 1174.60 172278.2 1473.25 185076.5      NA       NA
# 664 2017-10-25 1174.60 172278.2 1473.25 185076.5  -80.19 188974.1
# 663 2017-10-26 1174.60 172278.2 1473.25 185076.5  398.16 189372.3
# 662 2017-11-07 1174.60 172278.2 1473.25 185076.5 -348.37 189023.9

答案 2 :(得分:0)

也可以使用 imputeTS 包的na.locf函数。它有一个方便的 na.remaining 参数,可让您选择如何处理剩余的NA。在这种情况下很有帮助。

library("imputeTS")
Dax1Dax2Dax3$Accum1 <- na.locf(Dax1Dax2Dax3$Accum1, na.remaining = "rev")

这些是na.remaining参数的选项:

  • “保持”-返回带有NA的系列
  • “ rm”-删除剩余的NAs
  • “均值”-用总体均值替换剩余的资产净值
  • “ rev”-从相反方向执行nocb / locf