【问题标题】:how we can filter data in dataframe in R [closed]我们如何在 R 中过滤数据框中的数据 [关闭]
【发布时间】:2018-09-17 07:57:11
【问题描述】:
           f1      f2
3     11/2/16   56.25
5     11/3/16   56.25
7     11/4/16  111.00
10    12/5/16   13.00
16    11/8/16   35.00
19    11/9/16  415.21
22   11/10/16  280.00
23   12/10/16  817.00
24   10/11/16  830.00
25   11/11/16  644.00
28   11/12/16   90.00
31     2/1/17  250.00
33     3/1/17   45.00
34     3/1/17  184.00
35     4/1/17  578.16
36     4/1/17  160.00
37     5/1/17   21.00
39     6/1/17  352.00
41     6/1/17 2089.00
44     7/1/17  855.00
45     8/1/17  488.00
46     8/1/17  573.00
47     8/1/17  654.00
50     9/1/17 1995.00
55    11/1/17  115.00
56    11/1/17 2147.00
57    12/1/17   74.00
59    12/1/17 1431.00
60     1/2/17   50.00
62     2/2/17  657.00
67     5/2/17  629.00
71     6/2/17   23.00
75     8/2/17  350.00
77     8/2/17 1449.00
79     9/2/17  364.00
80     9/2/17 1185.00
85    11/2/17  405.00
86    11/2/17 4725.00
87    12/2/17   50.00
88    12/2/17  202.00
89    12/2/17 2377.00
90     1/3/17  500.00
91     2/3/17  600.00
93     3/3/17   60.00
94     3/3/17   14.00

具有上述数据集名称 dat.filtered。此数据必须转换为时间序列数据。 尝试此代码给出错误:

library(xts)
xts.sample <- xts(dat.filtered$f2, order.by = as.Date(dat.filtered$f1, "%d/%m/%Y"))

给出这个错误:

xts(dat.filtered$f2, order.by = as.Date(dat.filtered$f1, "%d/%m/%Y")) 中的错误: “order.by”不能包含“NA”、“NaN”或“Inf”

如何消除此错误。

【问题讨论】:

  • 你不需要在subset里面使用$;但它仍应按f2 &gt; 0 过滤条目。声明“但没有得到想要的结果” 不是很具体。什么没用?无论哪种方式,我都会在下面给出一个示例解决方案。

标签: r time-series


【解决方案1】:
# Filter
dat.filtered <- subset(dat, f2 > 0)

# Convert to time-series
library(xts);
xts.sample <- xts(dat.filtered$f2, order.by = as.Date(dat.filtered$f1, "%d/%m/%Y"))

样本数据

dat <- read.table(text =
    "                              f1              f2
1                         11/1/16               0
2                         12/1/16               0
3                         11/2/16           56.25
4                         12/2/16               0
5                         11/3/16           56.25
6                         12/3/16               0
7                         11/4/16             111
8                         12/4/16               0
9                         11/5/16               0
10                        12/5/16              13
11                        11/6/16               0
12                        12/6/16               0
13                        11/7/16               0
14                        12/7/16               0
15                        10/8/16               0
16                        11/8/16              35
17                        12/8/16               0
18                        10/9/16               0
19                        11/9/16          415.21
20                        12/9/16               0
21                       10/10/16               0
22                       11/10/16             280
23                       12/10/16             817
24                       10/11/16             830
25                       11/11/16             644
26                       12/11/16               0
27                       10/12/16               0
28                       11/12/16              90
29                       12/12/16               0
30                         1/1/17               0
31                         2/1/17             250
32                         2/1/17               0
33                         3/1/17              45
34                         3/1/17             184
35                         4/1/17          578.16
36                         4/1/17             160
37                         5/1/17              21
38                         5/1/17               0
39                         6/1/17             352
40                         6/1/17               0", header = T, row.names = 1)

【讨论】:

  • 如果 dat 数据集是 data.frame 我们该怎么做。
  • @Saurabh dat data.frame。运行我的示例。它是完全可重现的。
  • 非常感谢@Maurits Evers 我想问一下,如果我们有 data.frame 变量名作为 t 来代替文本,那么它会有什么变化。
  • @Saurabh 我不知道你在说什么。 “如果我们有 data.frame 变量名称作为 t 代替文本,那么它会有什么变化” 这是什么意思?我正在处理发布的示例数据。
  • @MauritsEvers 也许相关阅读:Should one advise on off-topic questions?; What is a help vampire?。不要让自己筋疲力尽;)
【解决方案2】:

我强烈建议您使用 dplyr 包进行过滤。 dplyr 中的功能非常直观。 要转换日期变量,请使用包 lubridate。

library(dplyr)
library(lubridate)

dat_cleaned <- dat %>%
                filter(f2 > 0
                #filter() function from dplyr
dat_cleaned$f1 <- dmy(dat_cleaned$f1)
#dmy() function from lubridate

【讨论】:

  • 鉴于 OP 与基本 R 函数和基本 R 对象的明显斗争,我不确定建议 OP 使用不同的库是否一定会使事情变得更容易。此外,似乎 OP 是在时间序列数据之后; dmy 产生 Date/POSIXct/POSIXlt 对象;那不是一回事。
  • 根据我的观点,从一开始就有几个包可以使用,dplyr 就是其中之一,因为 dplyr 中的函数构建起来非常一致,它们比许多基本的 R 函数更容易使用。如果您必须使用 lubridate 处理数据变量,那么使用基本 R 函数要容易得多。当然将字符格式的日期改成日期格式和时间序列分析是不一样的,但这是首先要做的,不是吗?
猜你喜欢
  • 2017-01-03
  • 2017-12-30
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
  • 1970-01-01
  • 2021-08-03
  • 2021-11-22
  • 2018-01-07
相关资源
最近更新 更多