如何修复is.data.frame(x):使用Pad时找不到对象''错误

时间:2019-09-23 17:47:25

标签: r

我一直在使用一些代码通过同时使用padfill_by_value的简单管道操作来填充时间序列数据中的空白。几个月以来,它一直很适合我,但今天它决定停止工作,并向我提供Error in is.data.frame(x) : object 'y' not found

有很多问题要解决这个错误。一些解决方案包括将产生错误的函数放在括号中(在这种情况下为fill_by_value),在函数上运行undebug(不适用于我,因为我的所有函数均未处于调试模式) ,然后将。$放在找不到的对象前面。这些解决方案都没有对我有用。

这是我要执行的操作的简化版本(我在pad函数说明的示例部分中找到了它,该方法也不起作用):

library(padr); library(dplyr) # for the pipe operator
month <- seq(as.Date('2016-04-01'), as.Date('2017-04-01'),
             by = 'month')[c(1, 4, 5, 7, 9, 10, 13)]
month_df <- data.frame(month = month, y = runif(length(month), 10, 20))

# fill all y with 0
month_df %>% pad %>% fill_by_value(y)

当应从此转换数据时,将产生错误Error in is.data.frame(x) : object 'y' not found

       month        y
1 2016-04-01 19.75770
2 2016-07-01 10.90725
3 2016-08-01 12.68625
4 2016-10-01 12.78528
5 2016-12-01 12.00717
6 2017-01-01 19.70017
7 2017-04-01 14.06676

对此:

        month        y
1  2016-04-01 19.75770
2  2016-05-01        0
3  2016-06-01        0
4  2016-07-01 10.90725
5  2016-08-01 12.68625
6  2016-09-01        0
7  2016-10-01 12.78528
8  2016-11-01        0
9  2016-12-01 12.00717
10 2017-01-01 19.70017
11 2017-02-01        0
12 2017-03-01        0
13 2017-04-01 14.06676

也许我只是不了解R的方式,但我不明白为什么这段简单的代码对我来说不再起作用。

1 个答案:

答案 0 :(得分:0)

您需要更改库的加载顺序。

Restarting R session...

> library(padr)
> library(EcoHydRology)
Loading required package: operators

Attaching package: ‘operators’

The following objects are masked from ‘package:base’:

    options, strrep

Loading required package: topmodel
Loading required package: DEoptim
Loading required package: parallel

DEoptim package
Differential Evolution algorithm in R
Authors: D. Ardia, K. Mullen, B. Peterson and J. Ulrich

Loading required package: XML
> library(dplyr) # for the pipe operator

Attaching package: ‘dplyr’

The following object is masked from ‘package:operators’:

    %>%

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

> month <- seq(as.Date('2016-04-01'), as.Date('2017-04-01'),
+              by = 'month')[c(1, 4, 5, 7, 9, 10, 13)]
> month_df <- data.frame(month = month, y = runif(length(month), 10, 20))
> # fill all y with 0
> month_df %>% pad %>% fill_by_value(y)
pad applied on the interval: month
        month        y
1  2016-04-01 19.88309
2  2016-05-01  0.00000
3  2016-06-01  0.00000
4  2016-07-01 17.46558
5  2016-08-01 10.34364
6  2016-09-01  0.00000
7  2016-10-01 13.82603
8  2016-11-01  0.00000
9  2016-12-01 17.36399
10 2017-01-01 19.55054
11 2017-02-01  0.00000
12 2017-03-01  0.00000
13 2017-04-01 15.62318