【问题标题】:Using filter function in R but receiving error message在 R 中使用过滤器功能但收到错误消息
【发布时间】:2018-09-21 23:46:29
【问题描述】:

这是我的数据框,我的原始数据框要大得多,但这只是一个例子:

df<-structure(list(ind = structure(c(16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16440, 
16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440, 
16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440, 
16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440, 
16440, 16440), class = "Date"), date = structure(c(17810, 17831, 
17852, 17873, 17894, 17915, 17936, 17957, 17978, 17999, 18020, 
18041, 18062, 18083, 18104, 18125, 18146, 18167, 18188, 18209, 
18230, 18251, 18272, 18293, 18314, 18335, 18356, 18377, 18398, 
18419, 18440, 18461, 18482, 18503, 18524, 18545, 18566, 18587, 
18608, 18629, 18650, 18671, 18692, 18713, 18734, 18755, 18776, 
18797, 18818, 18839, 18860, 18881, 18902, 18923, 18944, 18965, 
18986, 19007, 19028, 19049, 19070, 19091, 19112, 19133, 19154, 
19175, 19196, 19217, 19238, 19259, 19280, 19301, 19322, 19343, 
19364, 19385, 19406, 19427, 19448, 19469, 19490, 19511, 19532, 
19553, 19574, 19595, 19616, 19637, 19658, 19679, 19700, 19721, 
19742, 19763, 19784, 19805, 19826, 19847, 19868, 19889, 19910, 
19931, 19952, 19973, 19994, 20015, 20036, 20057, 20078, 20099, 
20120, 20141, 20162, 20183, 20204, 20225, 20246, 20267, 20288, 
20309, 17810, 17831, 17852, 17873, 17894, 17915, 17936, 17957, 
17978, 17999, 18020, 18041, 18062, 18083, 18104, 18125, 18146, 
18167, 18188, 18209, 18230, 18251, 18272, 18293, 18314, 18335, 
18356, 18377, 18398, 18419), class = "Date"), m = c(21L, 42L, 
63L, 84L, 105L, 126L, 147L, 168L, 189L, 210L, 231L, 252L, 273L, 
294L, 315L, 336L, 357L, 378L, 399L, 420L, 441L, 462L, 483L, 504L, 
525L, 546L, 567L, 588L, 609L, 630L, 651L, 672L, 693L, 714L, 735L, 
756L, 777L, 798L, 819L, 840L, 861L, 882L, 903L, 924L, 945L, 966L, 
987L, 1008L, 1029L, 1050L, 1071L, 1092L, 1113L, 1134L, 1155L, 
1176L, 1197L, 1218L, 1239L, 1260L, 1281L, 1302L, 1323L, 1344L, 
1365L, 1386L, 1407L, 1428L, 1449L, 1470L, 1491L, 1512L, 1533L, 
1554L, 1575L, 1596L, 1617L, 1638L, 1659L, 1680L, 1701L, 1722L, 
1743L, 1764L, 1785L, 1806L, 1827L, 1848L, 1869L, 1890L, 1911L, 
1932L, 1953L, 1974L, 1995L, 2016L, 2037L, 2058L, 2079L, 2100L, 
2121L, 2142L, 2163L, 2184L, 2205L, 2226L, 2247L, 2268L, 2289L, 
2310L, 2331L, 2352L, 2373L, 2394L, 2415L, 2436L, 2457L, 2478L, 
2499L, 2520L, 21L, 42L, 63L, 84L, 105L, 126L, 147L, 168L, 189L, 
210L, 231L, 252L, 273L, 294L, 315L, 336L, 357L, 378L, 399L, 420L, 
441L, 462L, 483L, 504L, 525L, 546L, 567L, 588L, 609L, 630L)), row.names = c(NA, 
-150L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), vars = "ind", drop = TRUE, indices = list(0:119, 120:149), group_sizes = c(120L, 
30L), biggest_group_size = 120L, labels = structure(list(ind = structure(c(16437, 
16440), class = "Date")), row.names = c(NA, -2L), class = "data.frame", vars = "ind", drop = TRUE))

我只想从“m”列中选择这个值,然后按“m”排列,这是我设置的命令:

df<-arrange(filter(df, m =="21"|m=="63"|m=="126"|m=="189"),m)

Error in is.data.frame(df) : object 'm' not found

还有其他方法可以获得相同的结果吗?为什么我有这个错误信息?

我做错了什么??

谢谢

【问题讨论】:

  • 尝试用df$m == 替换m ==
  • @Fábio 我有这个消息bf$m... Error in arrange(filter(df, df$m == "21" | df$m == "63" | df$m == "126" | : is.data.frame(df) is not TRUE
  • df$m 列不是数字列,对吧?
  • 也许您需要加载 dplyr?还有另一个名为 filter 的函数。
  • @Frank 是对的。我认为OP可能使用plyr包而不是dplyr

标签: r dplyr plyr


【解决方案1】:

这对我有用。您需要在运行以下代码之前重新启动您的 R 会话

library(dplyr)

df %>%
  filter(m == "21" | m == "63" | m == "126" | m == "189") %>%
  arrange(m)

# or
df %>%
  filter(m %in% c(21, 63, 126, 189) %>%
  arrange(m)

#> # A tibble: 8 x 3
#> # Groups:   ind [2]
#>   ind        date           m
#>   <date>     <date>     <int>
#> 1 2015-01-02 2018-10-06    21
#> 2 2015-01-05 2018-10-06    21
#> 3 2015-01-02 2018-11-17    63
#> 4 2015-01-05 2018-11-17    63
#> 5 2015-01-02 2019-01-19   126
#> 6 2015-01-05 2019-01-19   126
#> 7 2015-01-02 2019-03-23   189
#> 8 2015-01-05 2019-03-23   189

如果我们只加载 plyr 包,我们将收到与 OP 相同的错误消息

library(magrittr)
library(plyr)

df %>%
  filter(m == "21" | m == "63" | m == "126" | m == "189") %>%
  arrange(m)

#> Error in filter(., m == "21" | m == "63" | m == "126" | m == "189"): object 'm' not found

reprex package (v0.2.1.9000) 于 2018 年 9 月 21 日创建

【讨论】:

  • 还可以将此处的过滤器调用简化为:filter(m %in% c(21, 63, 126, 189)
  • @JayAchar:你是对的。我怎么能忘记那个:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 2022-11-17
相关资源
最近更新 更多